Linux学习
1.操作系统的实验基本上都是以linux下面的系统编程为内容,进程的调度和创建都是很基本的内容。很幸运的是,linux为我们提供了很多的系统函数,供我们来使用。通过熟悉这些函数,使得我们可以能够对进程和程序有一个更良好的认识。
2.Fork函数的使用。下面引用自百度百科:
fork()函数,Linux系统调用
头文件:
#include <unistd.h>
函数定义:
int fork( void );
返回值:
子进程中返回0,父进程中返回子进程ID,出错返回-1
函数说明:
一个现有进程可以调用fork函数创建一个新进程。由fork创建的新进程被称为子进程(child process)。fork函数被调用一次但返回两次。两次返回的唯一区别是子进程中返回0值而父进程中返回子进程ID。
子进程是父进程的副本,它将获得父进程数据空间、堆、栈等资源的副本。注意,子进程持有的是上述存储空间的“副本”,这意味着父子进程间不共享这些存储空间,它们之间共享的存储空间只有代码段。
示例代码:
#include <unistd.h>
#include <stdio.h>
int main(int argc, void ** argv )
{
int pid = fork();
if(pid == -1 ) {
// print("error!");
} else if( pid = =0 ) {
// print("This is the child process!");
} else {
// print("This is the parent process! child process id = %d", pid);
}
return 0;
}
|
1. 下面来详细的说说我的程序和问题,通过比较学习,相信对于linux的学习,特别是进程和程序直接的关系有一个更好的理解。
Process2.c
Code:
#include <stdio.h>
#include <unistd.h>
int main()
{
int p1;
p1=fork();
if (p1 == 0) //子进程,输出它的父进程和当前进程的PID
{
printf("current PID is %d\n",getpid());
printf("father PID is %d\n",getppid());
printf("\n");
}
else //这里是父进程吗?
{
printf("father PID is %d\n",getppid());
printf("current PID is %d\n",getpid());
printf("\n");
}
return 0;
}
分析:此进程的运行结果是?(思考后再回答)
是的,有两种情况。首先,程序运行到fork语句之后,创建进程,此时有两种情况,创建成功和不成功,而成功又有两种情况,fork的返回值为0,则运行的是创建的子进程,fork的返回值为1,则运行的是父进程。用if语句来判断,OK。
那么问题就是,if语句中没有问题,p1为0,说明创建成功,运行的是子进程,可是else语句来说,却又两种情况,如果创建不成功也在else语句中,那么此时当前进程就是那个原始的进程,而它的父进程却是1号PID,因为所有的进程都是1号进程的子进程,这个是linux下面的结论。操作系统的知识。
所以,此时的结果应该是:
father PID is 32667
current PID is 15254
current PID is 15255
father PID is 1
|
先放着,先解释下面的这个,注意,这个对上面是有帮助的。
(看完之后回头过来看看),看到没有,首先输出的那个father PID是什么,刚好就是下面的那个32667,这个不是偶然的,说明什么,当然,这个进程就是父进程,而创建它的那个进程的PID是32667,可以认为是系统创建的。就是说这种情况下面是,先运行的父进程。而它创建失败了,那么它没有子进程啊,那么那个下面的这个进程的父进程就不是上面的那个了,为何,从结果可以看到,它的PID是1,而不会是15254,说明不是上面的那个创建。而创建却是1,说明是系统创建的,所以fork语句之后,没有创建成功,而是由系统来亲自创建一个进程,所以PID号当然是下一个,不过父进程就是1号了。(有兴趣的可以查阅下资料,我们也是刚学,所以我还有继续探究中!!)
current PID is 15302 //子进程
father PID is 15301[l1]
father PID is 32667 //父进程
current PID is 15301
|
系统创建了当前的那个父进程,就是程序运行的进程
PID = 32667
|
下面还想讨论这样一个问题,这个问题是从上面那个第一种情况引申来的。
如下:
Code:
#include <stdio.h>
int main()
{
int p1;
while (( p1 = fork())!= -1);
if (p1 == -1)
{
printf("current PID is %d\n",getpid());
printf("father PID is %d\n",getppid());
}
return 0;
}[l2]
|
结果如下,虽然比较多,不过很能说明问题:
current PID is 17600
father PID is 1
current PID is 17761
father PID is 1
current PID is 17602
father PID is 1
current PID is 18545
father PID is 1
current PID is 17608
father PID is 1
current PID is 17767
father PID is 1
current PID is 17624
father PID is 1
current PID is 17773
father PID is 1
current PID is 17616
father PID is 1
current PID is 18551
father PID is 1
current PID is 17630
father PID is 1
current PID is 18552
father PID is 1
current PID is 17636
father PID is 1
current PID is 17779
father PID is 1
current PID is 17642
father PID is 1
current PID is 17785
father PID is 1
current PID is 17648
father PID is 1
current PID is 17787
father PID is 1
current PID is 17654
father PID is 1
current PID is 17795
father PID is 1
current PID is 18510
father PID is 1
current PID is 17801
father PID is 1
current PID is 18511
father PID is 1
current PID is 17807
father PID is 1
current PID is 17656
father PID is 1
current PID is 17815
father PID is 1
current PID is 17384
father PID is 32667
current PID is 17819
father PID is 1
current PID is 17660
father PID is 1
father PID is 1
current PID is 17825
father PID is 1
current PID is 17668
father PID is 1
current PID is 18571
father PID is 1
father PID is 1
father PID is 1
father PID is 1
father PID is 1
current PID is 17672
father PID is 1
current PID is 18572
father PID is 1
current PID is 17678
father PID is 1
current PID is 17831
father PID is 1
father PID is 1
current PID is 17684
father PID is 1
current PID is 17839
father PID is 1
current PID is 17690
father PID is 1
current PID is 17845
father PID is 1
current PID is 17696
father PID is 1
current PID is 17851
father PID is 1
current PID is 17702
father PID is 1
current PID is 17857
father PID is 1
current PID is 17710
father PID is 1
current PID is 17863
father PID is 1
current PID is 18531
father PID is 1
current PID is 17786
father PID is 1
current PID is 17714
father PID is 1
current PID is 18566
father PID is 17786
current PID is 17720
father PID is 1
current PID is 18533
father PID is 1
current PID is 17728
father PID is 1
current PID is 18538
father PID is 1
current PID is 17730
father PID is 1
current PID is 18548
father PID is 17740
current PID is 17738
father PID is 1
current PID is 17794
father PID is 1
current PID is 17740
father PID is 1
current PID is 17804
father PID is 1
current PID is 17750
father PID is 1
current PID is 17810
father PID is 1
current PID is 17756
father PID is 1
current PID is 17814
father PID is 1
current PID is 17764
current PID is 17820
father PID is 1
father PID is 1
current PID is 17768
father PID is 1
current PID is 17826
father PID is 1
current PID is 17774
father PID is 1
current PID is 17832
father PID is 1
current PID is 17780
father PID is 1
current PID is 17838
father PID is 17913
father PID is 1
father PID is 17929
father PID is 17927
father PID is 1
father PID is 17941
father PID is 1
father PID is 17950
father PID is 1
|
看完了上面的那个例子,下面的这个例子就是小case了,当然,其实上面的那个例子就是由下面的这个例子引申来的,是因为写错了代码而引起的。当然,根据错误,我们收获不小啊!
Code:
#include <stdio.h>
#include <unistd.h>
int main()
{
int p1;
while ((p1=fork())==-1) ;
if (p1 == 0)
{
printf("current PID is %d\n",getpid());
printf("father PID is %d\n",getppid());
printf("\n");
}
else
{
printf("father PID is %d\n",getppid());
printf("current PID is %d\n",getpid());
printf("\n");
}
return 0;
}
|
结果如下:
current PID is 20312[l3]
father PID is 20311
father PID is 32667
current PID is 20311
----------------------------------------------------------------------------------------------------------------
father PID is 32667
current PID is 20309[l4]
current PID is 20310
father PID is 1
|
[l1]父进程的PID刚好就是下面的那个父进程的current PID,说明下面的那个进程就是父进程。
[l2]看看这个程序,虽然比较流氓,不过可以根据结果知道,如果创建失败的话,那么也会有一个当前的PID,而且还是由一个1号进程创建。
[l3]当前是子进程,而它的父进程恰好是下面的这个进程!
[l4]父进程就是这个进程,而它的那个子进程却是由系统创建的!
后来通过查阅资料知道,如果父进程创建一个子进程后其先结束,那么子进程就变成了孤儿进程,这样的话,就会由初始化进程,即1号进程来领养。所以会显示它的父进程为1.至此,完美的解决了。。
最近在看《Linux程序设计》(第三版)一书,收获颇丰。本书是Linux下面程序设计的经典,值得一睹。。而且最近的感触是,懂得linux的操作和会在Linux下面进程程序设计完全是两回事,就像你会用windows和会开发windows下面的程序一样。我辈任重而道远啊。。
posted on 2009-12-10 14:41
deercoder 阅读(299)
评论(0) 编辑 收藏 引用 所属分类:
C/C++