Linuxプロセス

4656 ワード

#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
#include<stdlib.h>

int main()
{
    pid_t child_pid;
    child_pid = fork();	

    if (child_pid < 0) 
        printf("Error occured on forking./n");
    else if (child_pid == 0) { 
        /*     */
        printf("child process PID:%d
", (int)getpid()); exit(0); } else { /* */ printf("parent process PID:%d
", (int)getpid()); sleep(30); exit(0); } }

 
#include<stdio.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<unistd.h>
#include<stdlib.h>

int main()
{
    pid_t child_pid, release_pid;
    child_pid = fork();	

    if (child_pid < 0) 
        printf("Error occured on forking./n");
    else if (child_pid == 0) { 
        /*     */
        printf("child process PID:%d
", (int)getpid()); exit(0); } else { /* */ release_pid = wait(NULL); printf("parent process PID:%d
", (int)getpid()); printf("I catched a child process with PID of:%d
", release_pid); sleep(30); exit(0); } }

 
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
#include<stdlib.h>
#include<signal.h>

void handler(int num)
{
    int status;
    int pid = waitpid(-1, &status, WNOHANG);
    if (WIFEXITED(status)) 
        printf("The child %d exit with code %d
", pid, WEXITSTATUS(status)); } int main() { pid_t child_pid; child_pid = fork(); signal(SIGCHLD, handler); if (child_pid < 0) printf("Error occured on forking./n"); else if (child_pid == 0) { /* */ printf("child process PID:%d
", (int)getpid()); exit(3); } else { /* */ printf("parent process PID:%d
", (int)getpid()); int i; for (i = 0; i < 5; i++) { printf("do parent thing
"); sleep(3); } exit(0); } }

 
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/wait.h>

int main()
{
    pid_t child_pid, grandson_pid;
    child_pid = fork();	

    if (child_pid < 0) 
        printf("Error occured on forking./n");
    else if (child_pid == 0) { 
        /*   */
        grandson_pid = fork(); //     
        if (child_pid < 0) 
            printf("Error occured on forking./n");
        else if (grandson_pid > 0)
        {
            printf("child process PID:%d
", (int)getpid()); exit(0); // , init } else { /* */ printf("grandson process PID:%d
", (int)getpid()); sleep(2); exit(0); // ,init } } else { /* */ waitpid(child_pid, NULL, 0);// printf("parent process PID:%d
", (int)getpid()); sleep(30); exit(0); } }

 
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
#include<stdlib.h>
#include<signal.h>

int main()
{
    pid_t child_pid;
    child_pid = fork();
	
    signal(SIGCHLD, SIG_IGN);

    if (child_pid < 0) 
        printf("Error occured on forking./n");
    else if (child_pid == 0) { 
        /*     */
        printf("child process PID:%d
", (int)getpid()); exit(0); } else { /* */ printf("parent process PID:%d
", (int)getpid()); sleep(30); exit(0); } }