Linux系统.doc
上传人:qw****27 上传时间:2024-09-12 格式:DOC 页数:7 大小:81KB 金币:15 举报 版权申诉
预览加载中,请您耐心等待几秒...

Linux系统.doc

Linux系统.doc

预览

在线预览结束,喜欢就下载吧,查找使用更方便

15 金币

下载此文档

如果您无法下载资料,请参考说明:

1、部分资料下载需要金币,请确保您的账户上有足够的金币

2、已购买过的文档,再次下载不重复扣费

3、资料包下载后请先用软件解压,在使用对应软件打开

Linux系统下编程作业班级:姓名:学号:编写一个文件读写的例子,将自己的名字,学号写入一个文本文件中,并读出来。程序代码:#include<stdio.h>main(){FILE*fp;charch;if((fp=fopen("1.txt","wt+"))==NULL){printf("Cannotopenfilestrikeanykeyexit!");getch();exit(1);}printf("inputastring:\n");ch=getchar();while(ch!='\n')//这里用回车表示文件写入结束,你可以设置其他的字符,但是必须有结束的标示符。{fputc(ch,fp);ch=getchar();}rewind(fp);ch=fgetc(fp);while(ch!=EOF){putchar(ch);ch=fgetc(fp);}printf("\n");fclose(fp);}程序运行结果编写一个三线程的例子运行时间分别为100ms200ms300ms程序:#include<pthread.h>#include<stdio.h>#include<stdlib.h>Pthread_mutex_tmutex=PTHREAD_INITIALIZER;Pthread_cond_tcond=PEHREAD_COND_INITIALIZER;Void*thread1(void*);Void*thread2(void*);Void*thread3(void*);Inti=1;Intmain(){Pthread_tt1;Pthread_tt2;Pthread_tt3;Pthread_creath(&t1,NULL,.thread1,(void*)NULL);Pthread_creath(&t2,NULL,.thread2,(void*)NULL);Pthread_creath(&t3,NULL,.thread3,(void*)NULL);Pthread_join(t1,NULL);Pthread_join(t2,NULL);Pthread_join(t3,NULL);Exit(0);}{Printf(“线程1被创建”\n);Printf(“线程2被创建”\n);Printf(“线程3被创建”\n);For(i=1;i<=4;i++);If(i=1){Printf(“thread1:I’mthread1”\n)Sleep(0.1);Printf(“我已经延迟100毫秒了!”);}If(i=2)(Printf(thread2:I’mthread2”\n)Sleep(0.2)Printf(”我已经延迟200毫秒了!!”);}If(i=3){Printf(”I’mthread3”);Sleep(0.3);}Printf(”线程1已经结束\n”);Printf(”线程2已经结束\n”);Printf(”线程3已经结束\n”);}运行结果编写一个进程之间使用消息队列进行通信的例子程序:sen.c#include<stdio.h>#include<stdlib.h>#include<fcntl.h>#include<string.h>#include<sys/types.h>#include<sys/ipc.h>#include<sys/msg.h>#include<sys/stat.h>#defineBUF_SIZE256#definePROJ_ID32#definePATH_NAME"/tmp"#defineSEND_MSG1#defineRECEIVE_MSG2intmain(void){structmymsgbuf{longmsgtype;charmessage[BUF_SIZE];}msgbuf;intqueueid;intmsglen;if((queueid=msgget(1234,IPC_CREAT|0660))==-1){perror("msggeterror!\n");exit(1);}while(1){printf("send:");fgets(msgbuf.message,BUF_SIZE,stdin);//从标准输入读取字符存至messageif(strncmp("end",msgbuf.message,3)==0){msgctl(queueid,IPC_RMID,NULL);break;}msgbuf.message[strlen(msgbuf.message)-1]='\0';msgbu