processes 1 启动进程

beginning linux programming ch11
1.use system( ) in glibc
[root@localhost chapter11]# cat system1.c 
#include <stdlib.h>
#include <stdio.h>

int main()
{
    printf("Running ps with system\n");
    system("ps -e");
    printf("Done.\n");
    exit(0);
} 
[root@localhost chapter11]# ./system1
Running ps with system
  PID TTY          TIME CMD
    1 ?        00:00:04 init  
...
...
1382  ?        00:00:01 hald 14117 pts/0    00:00:00 system1
14118 pts/0    00:00:00 ps
Done.
[root@localhost chapter11]# 

if modify line8 int system1.c as follows
    system("ps -e &");
then
[root@localhost chapter11]# ./system1
Running ps with system
Done.
[root@localhost chapter11]#   PID TTY          TIME CMD
    1 ?        00:00:04 init
...
...
14159 pts/0    00:00:00 ps

[root@localhost chapter11]# 
可见
system1主线程在调用ps时,会等ps执行完毕返回后再继续执行
调用ps &时,不等ps返回就继续执行

2.exec system call
[root@localhost chapter11]# cat pexec.c 
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

int main()
{
    printf("Running ps with execlp\n");
    execlp("ps", "ps", "ax", 0);
    printf("Done.\n");
    exit(0);
}
[root@localhost chapter11]# ./pexec 
Running ps with execlp
  PID TTY      STAT   TIME COMMAND
...
14245 pts/0    R+     0:00 ps ax
[root@localhost chapter11]# 
进程pexec消失,ps出现,done没有被打印出来
可见exec会将当前进程替换为一个新进程
exec函数一共有六个,其中execve为内核级系统调用,其他(execl,execle,execlp,execv,execvp)都是调用execve的库函数。
       #include <unistd.h>

       extern char **environ;
       int execl(const char *path, const char *arg0, ... /*, (char *)0 */);
       int execv(const char *path, char *const argv[]);
       int execle(const char *path, const char *arg0, ... /*,
              (char *)0, char *const envp[]*/);
       int execve(const char *path, char *const argv[], char *const envp[]);
       int execlp(const char *file, const char *arg0, ... /*, (char *)0 */);
       int execvp(const char *file, char *const argv[]);

3.fork system call
[root@localhost chapter11]# cat test1.c 
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <error.h>
int main()
{
pid_t pid;
printf("getpid=%d\n",getpid());

char *str="hello";
printf("fork will start ----------\n");
pid=fork();
printf("fork alreaddy start ----------\n");
char *str_2="hello";
printf("pid=%d\n",pid);
printf("getpid=%d\n",getpid());

switch (pid)
	{
	case -1:
		perror("sorry\n");
		break;
	case 0:
		/*pid=0,child process*/
		printf("child\n");
		printf("child pid=%d\n",pid);
		printf("child getpid=%d\n",getpid());
		str="hellochilid";
		str_2="hellochilid";
		break;
	default:
		/*pid=the pid of son process(new pid),parent process*/
		printf("parent\n");
		printf("parent pid=%d\n",pid);
		printf("parent getpid=%d\n",getpid());
		str="helloparent";
		str_2="helloparent";
		break;
	}	
	printf("str=%s\n",str);
	printf("str_2=%s\n",str_2);
	pause();
}	
[root@localhost chapter11]# ./test1 
getpid=14536
fork will start ----------
fork alreaddy start ----------
pid=14537
getpid=14536
parent
parent pid=14537
parent getpid=14536
str=helloparent
str_2=helloparent
fork alreaddy start ----------
pid=0
getpid=14537
child
child pid=0
child getpid=14537
str=hellochilid
str_2=hellochilid


pause用来阻塞当前进程,相当于一直sleep
从输出结果可以看出两个进程的执行路线
在fork处,由一个进程变为俩
主进程在fork之前创建的变量都会在fork执行时给子进程复制一份,所以子进程拥有str
fork之前的代码不会复制给子进程,所以子进程成没有打印出fork will start ----------
fork之后的所有代码会复制给子进程(应该是共享)
所以fork之后的代码,子进程也会执行,所以子进程也拥有str_2
所以子进程的str和str_2的来源不同,前者是父进程复制给的,后者是自产的

ulk3
3.1. Processes, Lightweight Processes, and Threads

When a process is created, it is almost identical to its parent. It receives a (logical) copy of the parent's address space andexecutes the same code as the parent,beginning at the next instruction following the process creation system call. Although the parent and child may share the pages containing the program code (text), they have separate copies of the data (stack and heap), so that changes by the child to a memory location are invisible to the parent (and vice versa).


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值