Linux c学习--进程基础学习

简介:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
 
int  main( int  argc, char  const  *argv[])
{
     printf ( "I am process %ld\n" , ( long )getpid() );
     printf ( "My parent is %ld\n" , ( long )getppid());
     return  0;
}

 上面的例子输出了进程的ID和他的父进程的ID

#include <unistd.h>
#include <stdio.h>
 
int  main( int  argc, char  const  *argv[])
{
     printf ( "my real user id is %ld\n" , ( long )getuid());
     printf ( "my effective user id is %ld\n" ,( long )geteuid());
     printf ( "My real group id is %ld\n" , ( long )getgid());
     printf ( "My effective group id is %ld\n" ,( long )getegid() );
     return  0;
}
/*
this program run's result as follows :
my real user id is 1000
my effective user id is 1000
My real group id is 1000
My effective group id is 1000
*/

 上面的这个程序打印进程的各种ID和组ID

#include <unistd.h>
#include <stdio.h>
 
int  main( int  argc, char  const  *argv[])
{
     int  x;
     x=0;
     fork();
     printf ( "I am process %ld and my x is %d\n" , ( long )getpid() ,x);
     return  0;
}
 
/*
this program run's result as follows:
 
I am process 5416 and my x is 0
I am process 5417 and my x is 0
*/ <br><br>简单的fork的例子
#include <unistd.h>
#include <stdio.h>
 
int  main( int  argc, char  const  *argv[])
{
     pid_t childpid;
 
     childpid=fork();
     if (childpid== -1){
         perror ( "Failed to fork\n" );
         return  1;
     }
 
     if (childpid==0){
         printf ( "I am child %ld\n" , ( long )getpid());
     } else {
         printf ( "I am parent %ld\n" ,( long )getpid() );
     }
     return  0;
}
/*
this program run's result as follows:
I am parent 5467
I am child 5468
 
*/

 上面的例子输出了执行fork之后,父进程和子进程分别输出自身的ID

#include <unistd.h>
#include <stdio.h>
 
int  main( int  argc, char  const  *argv[])
{
     pid_t mypid;
     pid_t childpid;
 
     mypid=getpid();
     childpid=fork();
     if (childpid==-1){
         perror ( "Failed to frok\n" );
         return  1;
     }
 
     if (childpid==0){
         // child codes
         printf ( "I am child %ld, ID= %ld\n" ,( long )getpid(),( long )mypid);
     } else {
         // parent codes
         printf ( "I am parent  %ld, ID=%ld\n" ,( long )getpid(),( long )mypid );
     }
     return  0;
}
 
/*
this program run's result as follows:
 
I am parent  5517, ID=5517
I am child 5518, ID= 5517
 
*/

 父进程在调用fork之前,将mypid设置为自己的进程ID,执行fork之后,子进程获得了包含所有变量在内的父进程的地址空间的一份拷贝,由于子进程没有重置mypid,所以打印出的指不一样。

 

#include <stdio.h>
#include <unistd.h>
 
int  main( int  argc, char  const  *argv[])
{
     pid_t childpid=0;
     int  i,n;
 
     /*check for vaild number of common-line arguments*/
     if (argc!=2){
         fprintf (stderr, "usage: %s process \n" , argv[0] );
         return  1;
     }
 
     n= atoi (argv[1]);
     for  ( i = 0; i < n; ++i)
     {
         if (childpid=fork()){
             break ;
         }
 
         fprintf (stderr, "i:%d process ID: %ld parent ID: %ld child ID: %ld\n" , i,
             ( long )getpid(),( long )getppid(),( long )childpid);
     }
     return  0;
}
 
/*
this program run's result as follows:
 
i:0 process ID: 5603 parent ID: 1 child ID: 0
i:1 process ID: 5604 parent ID: 1 child ID: 0
i:2 process ID: 5605 parent ID: 1 child ID: 0
i:3 process ID: 5606 parent ID: 1 child ID: 0
*/

 上面的程序简单的创建了一个进程链。1->2->3->4...,另外大家要注意我在这里使用的是stderr,而不是stdout,因为系统会对写入stdout的进行缓冲。这样,特定的消息不能在printf之后立即显示。写入stderr则不会进行缓冲,而是立即写出。

 

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
 
int  main( int  argc, char  const  *argv[])
{
     pid_t childpid=0;
     int  i,n;
 
     if  (argc!=2)
     {
         fprintf (stderr, "usage: %s processes\n" ,argv[0] );
         return  1;
     }
 
     n= atoi (argv[1]);
     for  ( i = 0; i < n; ++i)
     {
         if ((childpid=fork())<=0){
             break ;
         }
         fprintf (stderr, "i:%d process ID:%ld, parent ID: %ld, child ID: %ld\n" ,
             i,( long )getpid(),( long )getppid(),( long )childpid );
     }
     return  0;
}

 上面的例子简单的创建了一个进程扇。

 

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
 
// this program will print some information after all the child process exits;
 
pid_t  r_wait( int * stat_lco);
 
int  main( int  argc, char  const  *argv[])
{
     pid_t childpid=0;
     int  i,n;
 
     if  (argc!=2)
     {
         fprintf (stderr, "usage: %s processes\n" ,argv[0] );
         return  1;
     }
 
     n= atoi (argv[1]);
     for  ( i = 0; i < n; ++i)
     {
         if ((childpid=fork())<=0){
             break ;
         }
         while (r_wait(NULL)>0);  /*wait for all of your children*/
         fprintf (stderr, "i:%d process ID:%ld, parent ID: %ld, child ID: %ld\n" ,
             i,( long )getpid(),( long )getppid(),( long )childpid );
     }
     return  0;
}
 
pid_t  r_wait( int * stat_lco){
     int  retval;
     while (((retval=wait(stat_lco)==-1)&& ( errno ==EINTR));
         return  retval;
}

 上面的进程扇使得所有子进程都推出之后,打印消息。

 

#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
 
// can you discribe all of this program run's results
 
int  main( int  argc, char  const  *argv[])
{  
     pid_t childpid;
 
     // set up signal handlers here
 
     childpid=fork();
     if (childpid==-1){
         perror ( "Faild to fork\n" );
         return  1;
     }
 
     if (childpid==0){
         fprintf (stderr, "I am child %ld\n" ,( long )getpid() );
     } else  if (wait(NULL)!=childpid){
         fprintf (stderr, "A signal must have interrupted the wait\n" );
     } else {
         fprintf (stderr, "I am parent %ld with child %ld\n" , ( long )getpid(),( long )childpid);
     }
 
     
     return  0;
}

 你能描述一下上面例子的所有可能输出吗?

 

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <errno.h>
 
int  main( int  argc, char  const  *argv[])
{
     pid_t childpid=0;
     int  i,n;
 
     if (argc!=2){
         fprintf (stderr, "Usage: %s processes\n" , argv[0] );
         return  1;
     }
 
     n= atoi (argv[1]);
     for  (i = 0; i < n; ++i)
     {  
         if ((childpid=fork())<=0){
             break ;
         }
         for (;;){
             childpid=wait(NULL);
             if ((childpid== -1)&&( errno  != EINTR)){
                 break ;
             }
         }
     }
     fprintf (stderr, "I am process %ld, my parent is %ld\n" ,( long )getpid(),( long )getppid() );
     return  0;
}

 上面的例子创建了一个进程扇,所有被创建的进程都是原始进程的子进程。

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
 
int  main( int  argc, char  const  *argv[])
{
     pid_t childpid;
     childpid=fork();
     if (childpid== -1){
         perror ( "Failed to fork\n" );
         return  1;
     }               /*child code*/
     if (childpid==0){
         execl( "/bin/ls" , "ls" , "-l" ,NULL);
         perror ( "child Failed to execl ls\n" );
         return  1;
     }               /*parent code*/
     if (childpid!=wait(NULL)){
         perror ( "parent Failed to wait due to signal or error\n" );
         return  1;
     }
     return  0;
}

 创建子进程来执行: ls -l命令。

目录
相关文章
|
存储 Linux C语言
Linux:入门学习知识及常见指令
Linux:入门学习知识及常见指令
|
4月前
|
资源调度 Linux 调度
Linux c/c++之进程基础
这篇文章主要介绍了Linux下C/C++进程的基本概念、组成、模式、运行和状态,以及如何使用系统调用创建和管理进程。
71 0
|
8月前
|
算法 Linux 编译器
技术笔记:LINUX2.6.32下的进程分析
技术笔记:LINUX2.6.32下的进程分析
43 0
|
8月前
|
安全 Linux 数据处理
深入探究Linux的pathchk命令
`pathchk`是Linux命令,用于检查文件名和路径的可移植性,遵循POSIX规范。它检测不可移植字符,确保文件在不同系统间的兼容性。选项如 `-p`, `-P` 和 `--portability` 提供不同级别的检查。基本用法是 `pathchk [选项] [文件名/路径名] [后缀]`。例如,`pathchk /etc/httpd/conf/httpd.conf` 检查路径的可移植性。使用时要注意目标系统的特性,谨慎处理警告,并结合其他命令如`find`使用。在操作前备份数据以防止损失。
|
9月前
|
编解码 Linux 数据安全/隐私保护
【Linux学习】​​学习Linux的准备工作和Linux的基本指令
【Linux学习】​​学习Linux的准备工作和Linux的基本指令
|
人工智能 Unix Linux
Linux入门 - 最常用基础指令汇总(1)
Linux入门 - 最常用基础指令汇总(1)
111 0
|
9月前
|
Linux 数据安全/隐私保护 UED
Linux的基础编程——命令基础(五)
Linux的基础编程——命令基础(五)
170 0
Linux的基础编程——命令基础(五)
|
9月前
|
Linux Shell 虚拟化
Linux的基础编程——命令基础(四)
Linux的基础编程——命令基础(四)
86 0
Linux的基础编程——命令基础(四)
|
搜索推荐 Linux Shell
哇!真的是你呀,让我看看Linux设置和命令基础大家是怎么学习的
Linux和Windows是两种不同的操作系统,所以两者之间的操作使用是不相同的,下面让我们一起去看看。
86 0
|
Linux 调度 容器
【Linux学习】进程概念
【Linux学习】进程概念
109 0