Linux application Development-process programming

Source: Internet
Author: User

You must understand the basic concepts before learning:

process, thread, process 3 states, process scheduling and 4 algorithms, process synchronization, deadlock, can't remember to find Niang.

Get ID

#include <sys/types.h>

#include <unistd.h>

Get this process ID:

pid_t getpid (void)

Get Parent Process ID:

pid_t getppid (void)

Example:

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

int main (void)

{

printf ("pid=%d\n", Getpid ());

printf ("pid=%d\n", Getppid ());

return 0;

}

Process creation:

#include <unistd.h>

pid_t fork (void)

Create a child process, be called once, return two times, possibly 3 kinds of values;

1. In the parent process, the child process PID is returned;

2. In the sub-process, return 0;

3. Error returns a negative value;

The data space for the child process, and the stack space is copied from the parent process, not shared.

-vfork

pid_t vfork (void)

Difference: The Vfork child process shares the data segment with the parent process.

The Vfork child process executes first, after the parent process.

The order of Fork is uncertain;

exec function Family:

The program being executed replaces the program that called it:

Difference:

Fork creates a new process that produces a new PID;

EXEC starts a new program, replaces the original process, and the PID does not change.

#include <unistd.h>

int execl (const char*path,const CHAR*ARG1,...);

Path: The name of the program being executed

ARGN: command-line arguments, with parameter names, ending with a null pointer (NULL)

Example:

#include <unistd.h>

Main ()

{

Execl ("/bin/ls", "ls", "-al", "/etc/passwd", (char*) 0);

}

#include <unistd.h>

int EXECP (const char*path,const CHAR*ARG1,...);

Path: The name of the program being executed (without the path, as found in the PATH environment variable).

Exmple

EXECP ("ls", "ls", "-al", "/etc/passwd", (char*) 0);

#include <unistd.h>

int Execv (const char*path, char*const argv[]);

Example

#include <unistd.h>

Main
{

char *argvl[]={"ls", "-al", "/etc/passwd", (char*) 0};

EXECV ("/bin/ls", argv);

}

#include <stdlib.h>

int system (const char*sring);

Call fork to produce a child process that calls/bin/sh-c string to execute the command represented by the argument string.

E:

System ("ls-al/etc/passwd");

Process waits:

#include <sys/types.h>

#include <sys/wait.h>

pid_t Wait (int*status)

Block the process until a process exits.

  

Linux application Development-process programming

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.