Linux第七章相关代码

胜利老师yyds
书上有些有错误,这里代码都已调好。

7-1

#include <unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<stdlib.h>
#include<stdio.h>
int main(void) {
	int fdl,fd2,fd3,nbytes;
	int flags=O_CREAT|O_TRUNC|O_WRONLY;
	char buf[10];
	if ((fdl=open ("rdwr.c", O_RDONLY, 0644)) <0) {
		perror("open rdwr.c");
		exit(EXIT_FAILURE);
		} 
		
		if ((fd2=open("/dev/null", O_WRONLY)) <0) {
			perror("open /dev/null");
			exit(EXIT_FAILURE);
		}
		
		if ((fd3=open("/tmp/foo.bar", flags, 0644)) <0) {
			perror("open /tmp/foo.bar");
			close(fdl);
			close(fd2);
			exit(EXIT_FAILURE);
			} 
			while((nbytes=read (fdl, buf,10)) >0) {
				if(write(fd2, buf, 10)<0)
					perror("write /dev/null");
				if(write(fd3,buf,nbytes)<0)
					perror("write /tmp/foo.bar");
				write(STDOUT_FILENO, buf, 10);
			}
			close(fdl);
			close(fd2);
			close(fd3);
			exit(EXIT_SUCCESS);
}

7-2

#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
struct person {
	char name[10];
	char id[10];
	off_t pos;
}people[]= {
	{"Zhangsan","123456",0},
	{"Lisi","246800",10240},
	{"Wangwu","135791",81920}
};
int main(int argc,char **argv){
	int fd,i,j;
	if(argc<2) {
		fprintf(stderr,"usage :%s file\n",argv[0]);
		return 1;
	}

	fd=open(argv[1],O_RDWR|O_CREAT|O_TRUNC,0666);
	if(fd<0) {
		fprintf(stderr, "%s:%s:cannot open for read/write:%s\n",
		argv[0],argv[1], strerror(errno));
		return 1;
	}
	j=sizeof(people)/sizeof(people[0]);
	for(i=0; i<j;i++) {
		if(lseek(fd,people[i].pos,SEEK_SET) <0 ) {
			fprintf(stderr,"%s:%s:seek error:% s \n", argv[0],argv[1],strerror(errno));
			close(fd);
			return 1;
		}
		if(write(fd,&people[i],sizeof(people[i]))!=sizeof(people[i])) {
			fprintf(stderr,"%s :%s :write error:%s\n",argv[0],argv[1], strerror(errno));
			close(fd);
			return 1;
		}
	}
	close(fd);
	return 0;
}

7-3

#include<stdio.h>
#include<errno.h>
#include<unistd.h>
#include<sys/types.h>
#include<stdlib.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("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-4

#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
int main(void) {
	pid_t child;
	int status, retval;
	if ((child=fork()) <0) {
		perror("fork");
		exit(EXIT_FAILURE);
	}

	printf("Child's PID:%d\n", child);

	if (child==0) {
		sleep(20);
		exit(EXIT_SUCCESS);
	} else {
		if((waitpid(child,&status,WNOHANG))==0) {
			retval=kill(child, SIGKILL);
			if(retval) {
				puts ("kill failed\n");
				perror("kill");
				waitpid (child, &status, 0);
			} else printf("%d killed\n", child);
		}
	}
	exit(EXIT_SUCCESS);
}

7-5

#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include<string.h>
int main(int argc, char **argv) {
	static const char mesg[] ="Happy New Years 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",argv[0],strerror(errno));
		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

#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFSZ 512
struct msg {
	long type;
	char text[BUFSZ];
};
int main(void) {
	int qid;
	key_t key;
	int lenl,len2;
	struct msg pmsg_w,pmsg_r;
	key=IPC_PRIVATE;
	if((qid=msgget(key, IPC_CREAT|0666))<0) {
		perror("msgget:create");
		exit (EXIT_FAILURE);
	}
	puts("Enter message to post:");
	if((fgets(pmsg_w.text,BUFSZ,stdin))==NULL) {
		puts("Wrong,no message to post.");
		exit (EXIT_FAILURE);
	}
	pmsg_w.type=10;
	lenl=strlen(pmsg_w.text);
	if((msgsnd(qid,&pmsg_w,lenl,IPC_NOWAIT))<0) {
		perror("msgsnd");
		exit(EXIT_FAILURE);
	}
	puts("message posted.");
	puts("**************");
	len2=msgrcv(qid,&pmsg_r,BUFSZ,10,IPC_NOWAIT|MSG_NOERROR);
	if(len2>0) {
		pmsg_r.text[len2]='\0';
		printf("reading queue id=%05d\n",qid);
		printf("message type=%05ld\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-7

#include <unistd.h> 
#include <sys/types.h> 
#include <wait.h> 
#include <signal.h> 
#include <stdio.h> 
#include <stdlib.h> 
int main(void){
	pid_t pid; 
	int num; 
if((pid=fork())<0){
	perror("fork"); 
	exit(EXIT_FAILURE);
}
else if(pid==0) 
sleep(30); 
else {
printf("Sending SIGCHLD to %d\n",pid); 
num=kill(pid,SIGCHLD); 
if(num<0) 
	perror("kill:SIGCHLD"); 
else 
	printf("%d still alive\n",pid); printf("Killing %d\n",pid); 
	if((kill(pid, SIGTERM))<0)
perror("kill:SIGTERM"); 
waitpid(pid,NULL,0); 
}
exit(EXIT_SUCCESS);
}

7-8

#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
int main(int argc,char **argv) {
	int i,fdl,fd2,nbytes;
	char buf[10];
	if(argc<3) {
		fprintf(stderr, "usage:%s origin destination\n"), argv[0];
		return 1;
	}
	
	if((fdl=open(argv[1],O_RDONLY,0644))<0) {
		fprintf(stderr, "cannot open %s for reading\n", argv[1]);
		exit(EXIT_FAILURE);
	}

	if((fd2=open(argv[2],O_WRONLY))<0) {
		fprintf(stderr,"cannot open %s for writing\n",argv[2]);
		exit(EXIT_FAILURE);
	}

	while((nbytes=read(fdl,buf,10))>0) {
		if(write(fd2,buf,nbytes)<0) {
			fprintf(stderr,"%s writing error!\n",argv[2]);
			exit(EXIT_FAILURE);
		}
		for(i = 0; i < 10; i++)
			buf[i]='\0';
	}
	close(fdl);
	close(fd2);
	system("echo ");
	system("echo显示当前目录--`pwd`--的内容");
	system("ls ");
	exit(EXIT_SUCCESS);
}

exam7-8

#!/bin/bash
#系统调用、C程序和shell 脚本的交叉调用
#这是shell 脚本
echo "今天是`date`"
if(($#!=2)); then
echo "exam7-8 的用法: exam7-8 文件1 文件2 "
exit 1
elif [ ! -f $1 -o ! -f $2 ]; then
echo "输入文件名有误!"
exit 2
else
#调用C程序rdwr
./rdwr $1 $2
fi
echo "下面是文件2--$2--的内容"
cat $2


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值