lunix实验六

习题7.3


书上例7.3


[root@localhost jincheng]# vi shiyan6.c
[root@localhost jincheng]# gcc shiyan6.c -o shiyan6
[root@localhost jincheng]# ./shiyan6
[root@localhost jincheng]# 
Original parent:19594
Child:19595
Child's old ppid:19594
Child's new ppid:1

[root@localhost jincheng]# cat shiyan6.c
#include<unistd.h>
#include<sys/types.h>
#include<stdio.h>
#include<errno.h>
#include<stdlib.h>
#include<string.h>
int main(int argc,char **argv)
{
pid_t pid,old_ppid,new_ppid;
pid_t child,parent;
parent=getpid();
if((child=fork())<0)
{
fprintf(stderr,"%s:fork of child failed:%s\n",argv[0],strerror(errno));
exit(1);

}
else if(child==0){
old_ppid=getppid();
sleep(2);
new_ppid=getppid();
}
else{
sleep(1);
exit(0);
}
printf("\n");
printf("Original parent:%d\n",parent);
printf("Child:%d\n",getpid());
printf("Child's old ppid:%d\n",old_ppid);
printf("Child's new ppid:%d\n",new_ppid);
exit(0);
}


习题7.3

编写一个程序,把一个文件的内容复制到另一个文件上,即实现简单的copy 功能。要求:只用open()、 read(), write()和close()系统调用,程序的第一个参数是源文件,第二个参数是目的文件。

先vi yuan.c 里面insert dajiahao! 在vi fuzhi.c 里面是空的

[root@localhost jincheng]# vi jin4.c
[root@localhost jincheng]# gcc jin4.c -o jin4
[root@localhost jincheng]# ./jin4
dajiahao!
[root@localhost jincheng]# cat fuzhi.c
dajiahao!
[root@localhost jincheng]# cat jin4.c
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<stdio.h>
#include<fcntl.h>
#include<stdlib.h>
int main(void)
{
int fd1,fd2,nbytes;
char buf[10];
if((fd1=open("yuan.c",O_RDONLY,0644))<0)
{
perror("open yuan.c");
exit(EXIT_FAILURE);
}
if((fd2=open("fuzhi.c",O_WRONLY))<0)
{
perror("open fuzhi.c");
exit(EXIT_FAILURE);
}
while((nbytes=read(fd1,buf,10))>0)
{
if(write(fd2,buf,10)<0)
perror("write fuzhi.c");
//STDOUT_FILENO 是unistd定义的标椎输出流 
write(STDOUT_FILENO,buf,10);
//作用是在屏幕显示输出内容
}
close(fd1);
close(fd2);
exit(EXIT_SUCCESS);
}

习题7.5

编写一个程序,首先打开一个文件,然后利用fork()创建一个子进程;随后,当父进程运行时先执行write();父子进程都打印自己和其父进程的ID号;并且二者都向该文件写入(利用write)一条信息,表明置在哪个进程中。试问:若没有wait()调用,则会出现什么情况?

[root@localhost jincheng]# vi shiyan6.c
[root@localhost jincheng]# gcc shiyan6.c -o shiyan6
[root@localhost jincheng]# ./shiyan6
parent.
parentchild.
child
Original parent:23030
Child:23031
parent's  ppid:21755
Child's ppid:23030
[root@localhost jincheng]# cat shiyan6.c
#include<unistd.h>
#include<sys/types.h>
#include<stdio.h>
#include<errno.h>
#include<stdlib.h>
#include<string.h>
#include<fcntl.h>
#include<sys/stat.h>
int main(int argc,char **argv)
{
int fd1;
pid_t pid,ppid;
pid_t child,parent,pparent;
parent=getpid();//获取父进程的id号
pparent=getppid();//获取父进程其父的ID号
if((child=fork())<0)//创建子进程
{
fprintf(stderr,"%s:fork of child failed:%s\n",argv[0],strerror(errno));
exit(1);
}
else if(child==0){//子进程创建成功
ppid=getppid();//父进程的id号
fd1=open("yuan.c", O_WRONLY);
		if (fd1 < 0)
		{
			perror("open");
			return -1;
		}
		
		printf("child.\n");
		write(fd1, "child", 6);
		write(STDOUT_FILENO, "child", 6);
}
else{
		fd1=open("yuan.c",O_WRONLY);
		if (fd1 < 0)
		{
			perror("open");
			return -1;
		}
		
		printf("parent.\n");
		write(fd1, "parent", 6);
			write(STDOUT_FILENO, "parent", 6);
		sleep(1);
exit(0);
}
printf("\n");
printf("Original parent:%d\n",parent);
printf("Child:%d\n",getpid());//获取子进程的id号
printf("parent's  ppid:%d\n",pparent);
printf("Child's ppid:%d\n",ppid);
exit(0);
}

例题7.5

[root@localhost jincheng]# vi jin5.c
[root@localhost jincheng]# gcc jin5.c -o jin5
[root@localhost jincheng]# ./jin5
Read end=fd 3,write end=fd 4
read <happy new year to you!> from pipe
[root@localhost jincheng]# cat jin5.c
#include<unistd.h>
#include<stdio.h>
#include<errno.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<string.h>
int main(int argc,char **argv)
{
static const char mesg[]="happy new year to you!";
char buf[BUFSIZ];
size_t rcount,wcount;
int p_fd[2];
size_t n;
if(pipe(p_fd)<0)
{
fprintf(stderr,"%s:pipe failed:%s\n",p_fd[0],p_fd[1]);
exit(1);
}
printf("Read end=fd %d,write end=fd %d\n",p_fd[0],p_fd[1]);
n=strlen(mesg);
if((wcount=write(p_fd[1],mesg,n))!=n)
{
fprintf(stderr,"%s:write failed :%s\n",argv[0],strerror(errno));
exit(1);
}
if((rcount=read(p_fd[0],buf,BUFSIZ))!=wcount)
{
fprintf(stderr,"%s:read failed :%s\n",argv[0],strerror(errno));
exit(1);
}
buf[rcount]='\0';
printf("read <%s> from pipe\n",buf);
close(p_fd[0]);
close(p_fd[1]);
return 0;
}

书上例7.6

[root@localhost jincheng]# vi jin7.c
[root@localhost jincheng]# gcc jin7.c -o jin7
[root@localhost jincheng]# ./jin7
enter message to post:
holle ^H^H^Hlunix
message posted.
***************
reading quque id=00000
message type=00010
message length=15 bytes
message text=hollunix

[root@localhost jincheng]# cat jin7.c
#include<unistd.h>
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/msg.h>
#include<string.h>
#include<sys/ipc.h>
struct msg{
long type;
char text[BUFSIZ];
};
int main(void){
int qid;
key_t key;
int len1,len2;
struct msg pmsg_w,pmsg_r;
key=IPC_PRIVATE;
if((qid=msgget(key,IPC_CREAT|0666))<0)
{
perror("msgget:creat");
exit(EXIT_FAILURE);
}
puts("enter message to post:");
if(fgets(pmsg_w.text,BUFSIZ,stdin)==NULL)
{
puts("Wrong,no message to post.");
exit(EXIT_FAILURE);
}
pmsg_w.type=10;
len1=strlen(pmsg_w.text);
if((msgsnd(qid,&pmsg_w,len1,IPC_NOWAIT))<0)
{
perror("msgsnd");
exit(EXIT_FAILURE);

}
puts("message posted.");
puts("***************");
len2=msgrcv(qid,&pmsg_r,BUFSIZ,10,IPC_NOWAIT|MSG_NOERROR);
if(len2>0)
{
pmsg_r.text[len2]='\0';
printf("reading quque id=%05d\n",qid);
printf("message type=%05d\n",pmsg_r.type);
printf("message length=%d bytes\n",len2);
printf("message text=%s\n",pmsg_r.text);
}
else{
perror("msgrcv");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}



习题7.9

[root@localhost jincheng]# gcc jin8.c -o jin8

[root@localhost jincheng]# 
[root@localhost jincheng]# ./jin8
pjincheng id=32137
cjincheng id=32138

 return status is 0xa
[root@localhost jincheng]# cat jin8.c
#include<unistd.h>
#include<stdio.h>
#include<stdlib.h>
#include<wait.h>
#include<signal.h>
void handler(int signo)
{
printf("\n cjincheng id=%d, fjincheng id=%d",getpid(),getppid());
}
int main(void){
int pid;

if((pid=fork())<0)
{
perror("fork");
exit(EXIT_FAILURE);
}
else if(pid==0)
{
pid=getpid();
signal(SIGUSR1,handler);
pause();
printf("child creat sucess\n");
exit(0);
}
else{
int status;
int childpid;
printf("pjincheng id=%d\n",getpid());
printf("cjincheng id=%d\n",pid);
kill(pid,SIGUSR1);
childpid=wait(&status);
printf("\n return status is 0x%x\n",status);
exit(EXIT_SUCCESS);
}
}

  • 6
    点赞
  • 71
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

临夏_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值