高级Linux程序设计第三章:进程
?
http://forfuture1978.iteye.com/blog/649981
http://forfuture1978.iteye.com/blog/652338
?
?
?
#include <stdio.h>
#include <unistd.h>
int main ()
{
??? printf (“The process ID is %d\n”, (int) getpid ());
??? printf (“The parent process ID is %d\n”, (int) getppid ());
??? return 0;
}
?
#include <stdlib.h>
int main ()
{
??? int return_value;
??? return_value = system (“ls -l /”);
??? return return_value;
}
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main ()
{
??? pid_t child_pid;
??? printf (“the main program process ID is %d\n”, (int) getpid ());
??? child_pid = fork ();
??? if (child_pid != 0) {
??????? printf (“this is the parent process, with id %d\n”, (int) getpid ());
??????? printf (“the child’s process ID is %d\n”, (int) child_pid);
??? }
??? else
??????? printf (“this is the child process, with id %d\n”, (int) getpid ());
??? return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
/* Spawn a child process running a new program. PROGRAM is the name of the program to run; the path will be searched for this program. ARG_LIST is a NULL-terminated list of character strings to be passed as the program’s argument list. Returns the process ID of the spawned process. */
int spawn (char* program, char** arg_list)
{
??? pid_t child_pid;
??? /* Duplicate this process. */
??? child_pid = fork ();
??? if (child_pid != 0)
??? /* This is the parent process. */
??????? return child_pid;
??? else {
??????? /* 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 ();
??? }
}
int main ()
{
??? /* The argument list to pass to the “ls” command. */
??? char* arg_list[] = {
??????? “ls”, /* argv[0], the name of the program. */
??????? “-l”,
??????? “/”,
??????? NULL /* The argument list must end with a NULL. */
??? };
??? /* Spawn a child process running the “ls” command. Ignore the returned child process ID. */
??? spawn (“ls”, arg_list);
??? printf (“done with main program\n”);
??? return 0;
}
?
#include <stdio.h>
#include <signal.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
sig_atomic_t sigusr1_count = 0;
void handler(int signal_number)
{
++sigusr1_count;
}
int main(int argc, char* argv[])
{
??? printf("the process ID is %d\n", (int)getpid());
??? struct sigaction sa;
??? memset(&sa, 0, sizeof(sa));
??? sa.sa_handler = &handler;
??? sigaction(SIGUSR1, &sa, NULL);
??? int i = 0;
??? while(i < 100)
??? {
??????? sleep(1);
??????? i++;
??? }
??? printf("SIGUSR was raised %d times\n", sigusr1_count);
??? return 0;
}
编译上述代码为程序sigusr1
gcc -o sigusr1 sigusr1.c
运行程序
[liuchao@localhost Signal]$ ./sigusr1
the process ID is 3401
在另一个终端,用ps命令得到sigusr1的进程号
[liuchao@localhost ~]$ ps -a
PID TTY TIME CMD
3401 pts/1 00:00:00 sigusr1
3403 pts/3 00:00:00 ps
向此进程号发送多个sigusr1信号
[liuchao@localhost ~]$ kill -s SIGUSR1 3401
[liuchao@localhost ~]$ kill -s SIGUSR1 3401
[liuchao@localhost ~]$ kill -s SIGUSR1 3401
[liuchao@localhost ~]$ kill -s SIGUSR1 3401
[liuchao@localhost ~]$ kill -s SIGUSR1 3401
当进程结束后
[liuchao@localhost Signal]$ ./sigusr1
the process ID is 3401
SIGUSR was raised 5 times
?
?
% kill -KILL pidint main ()
{
??? int child_status;
??? /* The argument list to pass to the “ls” command. */
??? char* arg_list[] = {
??????? “ls”, /* argv[0], the name of the program. */
??????? “-l”,
??????? “/”,
??????? NULL /* The argument list must end with a NULL. */
??? };
??? /* Spawn a child process running the “ls” command. Ignore the returned child process ID. */
??? spawn (“ls”, arg_list);
??? /* Wait for the child process to complete. */
??? wait (&child_status);
??? if (WIFEXITED (child_status))
??????? printf (“the child process exited normally, with exit code %d\n”, WEXITSTATUS (child_status));
??? else
??????? printf (“the child process exited abnormally\n”);
??? return 0;
}
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
int main ()
{
??? pid_t child_pid;
??? /* Create a child process. */
??? child_pid = fork ();
??? if (child_pid > 0) {
??????? /* This is the parent process. Sleep for a minute. */
?????? sleep (60);
??? }
??? else {
??????? /* This is the child process. Exit immediately. */
??????? exit (0);
??? }
??? return 0;
}
% ps -e -o pid,ppid,stat,cmd
3824 2888 S+ ./zombie
3825 3824 Z+ [zombie] <defunct>
#include <signal.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
sig_atomic_t child_exit_status;
void clean_up_child_process (int signal_number)
{
??? /* Clean up the child process. */
??? int status;
??? wait (&status);
??? /* Store its exit status in a global variable. */
??? child_exit_status = status;
}
int main ()
{
??? /* Handle SIGCHLD by calling clean_up_child_process. */
??? struct sigaction sigchld_action;
??? memset (&sigchld_action, 0, sizeof (sigchld_action));
??? sigchld_action.sa_handler = &clean_up_child_process;
??? sigaction (SIGCHLD, &sigchld_action, NULL);
??? /* Now do things, including forking a child process. */
??? /* ... */
??? return 0;
}
?