Lab 5-Process Management

Source: Internet
Author: User

Experimental Purpose:

Familiar with the application of process creation functions in Linux.

Experimental content:

Program 1:

    • Complete the creation of the child process with the fork () function.
    • Output of word in parent process, child process output Hello
    • Run the Hibernate function to implement the child process first
1#include <stdio.h>2#include <sys/types.h>3#include <unistd.h>4#include <stdlib.h>5 intMain ()6 {7 pid_t pid;8     if(PID = fork ()) = =0)9     {Ten         //Child Process Oneprintf"hello\n"); AExit0); -     } -     Else if(PID >0) the     { -         //Parent Process -Sleep5); -printf"word\n"); +Exit0); -     } +     Else A     { atprintf"fork () error\n"); -Exit0); -     } -}

The fork () call failed to return-1.

There are two main reasons why the fork function call failed:

    • There are already too many processes in the system
    • The total number of processes for the actual user ID exceeds the system limit

The fork call is successfully returned two times.

    • In the parent process, it returns once, and the return value is the newly derived child process ID number
    • It also returns one time in a child process, with a return value of 0, so you can distinguish whether the current process is a child or parent process by the return value.

After the fork is called, the parent and child processes continue to execute the instruction after the fork function, whether the parent process executes first or the child process is indeterminate,

Lab 5-Process Management

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.