Process and Classroom Summary

Source: Internet
Author: User
Tags function definition

2015.1.29

When the parent process waits for a child process to terminate, it must call the wait function. If a process waits for the parent process to terminate, you can do the following: The disadvantage is that CPU time is wasted
while (Getppid ()! = 1)
Sleep (1);

IPC: Inter-process communication

int main ()
{
pid_t pid;
if ((PID = fork ()) < 0)
{
Err_sys ("fork Error");
}
else if (PID = = 0)
{
if ((PID = fork ()) < 0)
{
Err_sys ("fork Error");
}
else if (PID > 0)
{
Exit (0);
}

Sleep (2);
printf ("second chile,parent pid =%d\n", Getppid ());
Exit (0);
}

if (Waitpid (pid,null,0)! = pid)
{
Err_sys ("Waitpid error");
}
Exit (0);
}

The results of the above program operation: Second chile,parent pid = 1;

Each process notifies the other after performing his set of initialization operations, and waits for the other party to complete its initialization before continuing:

#include "apue.h"
Tell_wait (); Tell ****wait***
if ((PID = fork ()) < 0)
{
Err_sys ("fork Error");
}
else if (pif = = 0)
{
Tell_parent (Getppid ()); /*tell Parent we ' re done*/
Wait_parent (); /*and Wait for parent*/
Exit (0);
}
Tell_child (PID);
Wait_chile ();
Exit (0);

Programs with competitive conditions:
#include <stdio.h>
static void Charartatime (char *);
int main (void)
{
pid_t pid;
if ((PID = fork ()) < 0)
{
Err_sys ("fork Error");
}
else if (PID = = 0)
{
Charatatime ("Output from child\n");
}
Else
{
Charatatime ("Output from parent\n");
}
Exit (0);
}

static void Charatatime (char *str)
{
Char *ptr;
int C;
Setbuf (Stdout,null);
for (ptr = str; (c = *ptr++)! = 0;)
{
PUTC (c,stdout);
}
}

Modify the program to avoid competition: The parent process runs first
#include <stdio.h>
static void Charartatime (char *);
int main (void)
{
pid_t pid;
Tell_wait (); Tell ****wait***

if ((PID = fork ()) < 0)
{
Err_sys ("fork Error");
}
else if (PID = = 0)
{
Wait_parent (); /*parent goes first*/
Charatatime ("Output from child\n");
}
Else
{
Charatatime ("Output from parent\n");
Tell_child (PID);
}
Exit (0);
}

static void Charatatime (char *str)
{
Char *ptr;
int C;
Setbuf (Stdout,null);
for (ptr = str; (c = *ptr++)! = 0;)
{
PUTC (c,stdout);
}
}

Change the program above to let the child process run first:

else if (PID = = 0)
{
Charatatime ("Output from child\n");
Tell_parent (Getppid ());
}
Else
{
Wait_chile (); /*child goes first*/
Charatatime ("Output from parent\n")
}

Recall the lesson today: the morning review of the knowledge points of yesterday's pointers, including: the basis of pointers, pointer operations, multi-level pointers, pointers array, const,
Void, character pointers, and strings, chuanjiang the connections and usages between pointers and arrays. The main point today is that the function of this piece of knowledge, including: function definition and declaration
function invocation, parameter passing, and return value, function and array, main function parameter, pointer function, recursive function. Finally, a little bit of knowledge about structure. Tell me about today.
The lesson of the harvest, recursive function I understand, the function of the method of the transfer of parameters I understand that today, from the stack and memory point of view to further understand the nature of function parameters, pointer array
The array pointers are further understood, with a further understanding of the subsequent assignment, int *p[2], int a[2][3], p[0] = a[0]. This period of time C, from
The point of view of knowledge, very few I do not know, but still need to practice more good points. This time of the class let me very relaxed, this gave me a good buffer time, before the holiday I want to read
UNIX Environment Advanced Programming This book, so I can play arm development Board after home, this is what I want to play!

Process and Classroom Summary

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.