#include <stdio.h> // for printf()
#include <unistd.h> // for getpid(), getppid()
int main()
{
printf ("The process ID is %d\n", (int) getpid());
printf ("The parent process ID is %d\n", (int) getppid());
return 0;
}
/*
gcc print-pid.c -o print-pid
./print-pid
The process ID is 75713
The parent process ID is 75593
./print-pid
The process ID is 77557
The parent process ID is 75593
*/
*********************************************************************************
*********************************************************************************
*********************************************************************************
*********************************************************************************
*********************************************************************************
#include <stdlib.h> // for system()
int main()
{
system("ls -l");
return 0;
}
/*
gcc system.c -o system
./system
total 24
-rwxrwxr-x 1 user user 16696 Sep 15 20:16 system
-rw-rw-r-- 1 user user 125 Sep 15 20:16 system.c
*/
*********************************************************************************
*********************************************************************************
*********************************************************************************
*********************************************************************************
*********************************************************************************
#include <stdio.h> // for printf()
#include <sys/types.h> // for pid_t
#include <unistd.h> // getpid(), fork()
int main()
{
pid_t childid;
printf ("The main program process ID is %d\n", (int)getpid());
childid = fork(); // fork() returns the child PID to the parent
if (childid != 0)
{
printf ("This is the parent process, with id %d\n", (int)getpid());
printf ("The child's process ID is %d\n", (int)childid);
}
else // fork() returns 0 to the child process
{printf("This is the child process, with id %d\n", (int)getpid());}
return 0;
}
/*
gcc fork.c -o fork
./fork
The main program process ID is 90652
This is the parent process, with id 90652
The child's process ID is 90653
This is the child process, with id 90653
*/
*********************************************************************************
*********************************************************************************
*********************************************************************************
*********************************************************************************
*********************************************************************************
#include <stdio.h> // for printf(), fprintf(), stderr, NULL
#include <stdlib.h> // for abort()
#include <sys/types.h> // for pid_t
#include <unistd.h> // for fork(), execvp()
int spawn (char * program, char ** arg_list);
// Spawn a child process running a new program
int main()
{
/* The argument list to pass to the `ls' command: */
char * arg_list[] =
{
"ls", /* argv[0], the name of the program */
"-l", // long listing
// "/", // root directory
NULL /* The argument list must end with a NULL */
};
/* Spawn a child process running the "ls" command: */
spawn ("ls", arg_list); // Ignore the returned child process ID
printf ("Done with main program\n");
return 0;
}
int spawn (char * program, char ** arg_list)
{
pid_t child_pid;
child_pid = fork(); /* Duplicate this process */
if (child_pid != 0) /* This is the parent process. */
{return child_pid;} // the returned value is ignored in main()
else // child process
{ /* Now execute `program', searching for it in the path: */
execvp (program, arg_list);
/* The execvp() function returns only if an error occurs: */
fprintf (stderr, "An error occurred in execvp()\n");
abort(); // end program
}
}
/*
gcc fork-exec.c -o fork-exec
./fork-exec
Done with main program
$ total 24
-rwxrwxr-x 1 user user 17000 Sep 15 23:41 fork-exec
-rw-rw-r-- 1 user user 2504 Sep 15 23:42 fork-exec.c
// Ctrl^C
*/