《LINUX C编程一站式学习》百度网盘pdf最新全集下载:
链接:https://pan.baidu.com/s/1wIgN0sfCc9gjkgPdARtUng
?pwd=4jut 提取码:4jut简介:通俗易懂的原理剖析,有非常适合初学者的code,有形象化的展示。本书分为两部分:第一部分讲解编程语言和程序设计的基本思想方法,让读者从概念上认识C语言。
#include<stdio.h>main()
{int p1,p2
printf("in parent when no child\n")
system("ps -af")
while ((p1=fork())==-1)
if(p1==0)
{printf("hello I am the first child!\n")
printf("in child1:pid:%d,ppid:%d\n",getpid(),getppid())
system("ps -af")}
else
{printf("in parent when one child:\n")
system("ps -af")
while((p2=fork())==-1)
if(p2==0)
{printf("hello I am the second child!\n")
printf("in child2:pid:%d,ppid:%d\n",getpid(),getppid())
system("ps -af")
}
else{printf("hello I am the parent!\n")
printf("in parent when two chileren: pid:%d,ppid:%d\n",getpid(),getppid())
}
}
}
还给你一个
#include<fcntl.h>
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
int fatal(const char* info)
{printf("error!!!\n")
exit(0)
return(0)}
int lock(int fd)
{lseek(fd,0,SEEK_SET)
if(lockf(fd,F_LOCK,0)==-1)
fatal("lockf()")
return 0
}
int unlock(int fd)
{lseek(fd,0,SEEK_SET)
if(lockf(fd,F_ULOCK,0)==-1)
fatal("unlockf()")
return 0
}
int main()
{int pid,fd
char str[80]
fd=open("tep.txt",O_CREAT,0644)
pid=fork()
switch(pid)
{
case -1:
fatal("fork fail!")
case 0:sleep(1)
lock(fd)
lseek(fd,SEEK_SET,0)
read(fd,str,sizeof(str))
unlock(fd)
printf("son %d:read str from tmpfile:%s\n",getpid(),str)
exit(0)
default:
lock(fd)
printf("parent %d:please enter a str for tmpfile(strlen<80):\n",getpid())
scanf("%s",str)
lseek(fd,SEEK_SET,0)
write(fd,str,strlen(str))
unlock(fd)
wait(0)
close(fd)
exit(0)
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)