include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
int process_id;
char process_id_from_file[10];
FILE *pid_fd;
char command[100];
sprintf(command,"pidof %s", argv[1]);
pid_fd = popen(command, "r");
if (pid_fd == NULL)
{
// perror("Error in reading pgrep %s", process_name);
perror("Error in reading pgrep ");
return -1;
}
if (fgets(process_id_from_file, sizeof process_id_from_file , pid_fd) == NULL)
{
return -1;
}
pclose(pid_fd);
process_id = atoi(process_id_from_file);
if (process_id != 0)
{
printf("\nprocess id : %d \n", process_id);
}
return 0;
}
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
int process_id;
char process_id_from_file[10];
FILE *pid_fd;
char command[100];
sprintf(command,"pidof %s", argv[1]);
pid_fd = popen(command, "r");
if (pid_fd == NULL)
{
// perror("Error in reading pgrep %s", process_name);
perror("Error in reading pgrep ");
return -1;
}
if (fgets(process_id_from_file, sizeof process_id_from_file , pid_fd) == NULL)
{
return -1;
}
pclose(pid_fd);
process_id = atoi(process_id_from_file);
if (process_id != 0)
{
printf("\nprocess id : %d \n", process_id);
}
return 0;
}