1、查看进程:ps -af(系统进程:ps -ax)
#include
#include
int main()
{
printf("Running ps with system!\n");
system("ps -ax");
printf("Done!\n");
return 0;
}
2、替换进程映像exec:
#include
int main()
{
printf("Starting!\n");
execlp("ps", "ps", "-ax", 0);
printf("Done!\n");
}
3、复制进程映像fork:
#include
#include
#include
int main()
{
pid_t pid;
char *message;
int n;
printf("fork program starting!\n");
pid = fork();
switch (pid)
{
case -1:
perror("fork failed!");
exit(1);
case 0:
message = "This is the child!\n";
n = 5;
break;
default:
message = "This is the parent!\n";
n = 3;
break;
}
for (; n > 0; n--)
{
puts(message);
sleep(1); // usleep(1000); // 1 second
}
exit(0);
}
#include
#include
#include
int main()
{
pid_t pid;
char *message;
int n;
int exit_code = 0;
printf("fork program starting!\n");
pid = fork();
switch (pid)
{
case -1:
perror("fork failed!");
exit(1);
case 0:
message = "This is the child!\n";
n = 5;
exit_code = 34;
break;
default:
message = "This is the parent!\n";
n = 1;
exit_code = 55;
break;
}
for (; n > 0; n--)
{
puts(message);
sleep(1); // usleep(1000); // 1 second
/* This section of the program waits for the child process to finish. */
if (pid)
{
int stat_val;
pid_t child_pid;
child_pid = wait(&stat_val);
printf("Child has finished:PID = %d\n", child_pid);
if (WIFEXITED(stat_val))
{
printf("Child exit with code:%d\n", WEXITSTATUS(stat_val));
}
else
{
printf("Child terminated abnormally!\n");
}
}
}
exit(exit_code);
}
4、僵进程