c语言编译进程退出返回值0,C语言中关于进程的函数

一、取得进程ID

头文件:#include

1. pid_t getpid(void);得到进程ID

2.pid_t getppid(void);得到父进程ID

3.pid_t getuid(void);得到用户ID

4.pid_t geteuid(void);得到有效用户ID

5.pid_t getgid(void);得到组ID

6.pid_t getegid(void);得到有效组ID

说明:进程标识符的类型是pid_t,其本质是无符号整形

对一般进程而言,用户ID与有效用户ID是一样的(组ID也一样)。但也有例外。

示例:

#include #include int main()

{

pid_t pid,ppid,uid,euid,gid,egid;

pid=getpid();

ppid=getppid();

uid=getuid();

euid=geteuid();

gid=getgid();

egid=getegid();

printf("id of current process: %d\n",pid);

printf("parent id of current process: %d\n",ppid);

printf("user id of current process: %d\n",uid);

printf("effective user id of current process: %d\n",euid);

printf("group id of current process: %d\n",gid);

printf("effective group id of current process: %d\n",egid);

return 0;

}

结果:

id of current process: 14807

parent id of current process: 8924

user id of current process: 1000

effective user id of current process: 1000

group id of current process: 1000

effective group id of current process: 1000

二、设置进程ID

1. setuid()

声明:int setuid( uid_t uid );

头文件:#include

功能:修改用户ID和有效用户ID,参数uid表示改变后的新用户ID和有效用户ID。

返回值:如果成功修改当前进程的用户ID和有效用户ID,返回0;否则返回-1

2. seteuid()

声明:int seteuid( uid_t uid );

头文件:#include

功能:仅修改有效用户ID,参数uid表示改变后的有效用户ID。

返回值:如果成功修改当前进程有效用户ID,返回0;否则返回-1

3. setgid()setegid()

声明:int setgid( gid_t gid ); int setegid( gid_t gid );

头文件:#include

功能:修改组ID和有效组ID

返回值:如果成功修改返回0;否则返回-1

三、创建进程

1. fork()

声明:pid_t fork( void );

头文件:#include

功能:创建一个新进程,得到一个新的可用进程ID。并将父进程的数据段和堆栈段复制到子进程的进程空间,并且和父进程共享代码段(代码段是只读的,不存在修改的问题)。

返回值:对于父进程,返回新创建子进程的ID;对于子进程,返回0;出错返回-1

说明:fork()产生的子进程与父进程地位平等,先执行哪一个由系统决定。

2. vfork()

声明:pid_t vfork( void );

头文件:#include

功能:创建一个新进程,得到一个新的可用进程ID。vfork()产生的子进程和父进程完全共享地址空间,包括代码段、数据段和堆栈段,子进程对这些资源的修改可以影响父进程。所以vfork()函数与其说产生了一个进程,不如说产生了一个线程。

返回值:对于父进程,返回新创建子进程的ID;对于子进程,返回0;出错返回-1

说明:vfork()产生的子进程一定比父进程先运行,即父进程调用了vfork()函数后会等待子进程运行结束后再运行。

示例: #include #include #include int global;    //全局变量,在数据段中

int main()

{

pid_t pid;

int stack=1;    //局部变量,在栈中

int * heap;

heap=(int*)malloc(sizeof(int));    //动态分配的内存,在堆中

*heap=2;

pid=fork();

//    pid=vfork();

if(pid<0)

{

printf("fail to fork\n");

exit(1);

}

else if(pid==0)

{

global++;

stack++;

(*heap)++;

printf("this is child, pid is: %d\n",getpid());

printf("the child, data: %d, stack: %d, heap: %d\n",global,stack,*heap);

exit(0);    //退出子进程

}

sleep(1);    //fork()时父进程休眠1秒,保证子进程先运行;vfork()产生的子进程一定比父进程先运行,所以不需要此语句

printf("this is parent, pid is: %d, child-pid is: %d\n",getpid(),pid);

printf("the parent, data: %d, stack: %d, heap: %d\n",global,stack,*heap);

return 0;

}

结果:

1.fork()

this is child, pid is: 12609

the child, data: 1, stack: 2, heap: 3

this is parent, pid is: 12608, child-pid is: 12609

the parent, data: 0, stack: 1, heap: 2

2.vfork()

this is child, pid is: 5464

the child, data: 1, stack: 2, heap: 3

this is parent, pid is: 5463, child-pid is: 5464

the parent, data: 1, stack: 2, heap: 3

四、退出进程

exit()

声明:void exit( int status );

头文件:#include

功能:参数status是该程序的退出状态,如果正常退出参数为0;异常退出为非0

返回值:无返回值

说明:C程序中的return语句会被编译器翻译为exit()函数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值