2) 学习并掌握Linux的文件操作3) 编写并实现实验步骤6要求的程序2、实验内容和步骤步骤1:使用Vi将程序清单3-1和3-2的程序输入,并在当前目录下创建文件“file.in”和文件“file.out”,尽可能的使文件“file.in”大一些步骤 2:利用gcc分别编译这两个程序,写出编译命令和执行结果,如果不成功,尝试利用gdb调试gcc –o list3_1 list3_1.cgcc –o list3_2 list3_2.c步骤3:仔细观察这两个程序,比较标准C的文件操作和Linux的系统调用open、read、write等的使用区别答:标准c的文件操作一个是库函数的调用,linux中系统调用文件操是底层的文件调用,其运行效率略有差距,同时参数也不同,返回的数据类型也不同步骤4:按照说明重新修改程序3-2,并使用time命令察看程序执行的时间效率上有何区别 修改前:修改后:步骤5:输入、编译并运行程序3-3和3-4,写出执行结果,并比较他们fseek和lseek在使用方法上的异同两者都是对文件的读写指针进行设置但两者的返回类型不同,fseek返回的是int型lseek返回的是off_t型步骤6:学习并分别使用标准C的文件操作函数和Linux的系统调用创建一个对学生基本信息进行操作(插入、修改和删除)的C程序,学生基本信息以结构体的形式存储在文件stu.info中,struct stu_info的定义如下:struct stu_info { char stu_num[12]; char name[10]; short int sex;/*0为女生,1为男生*/ char mobile_phone[12];};3、 实验结论通过对文件操作的编程,让我掌握了文件的创建,打开,读写,以及特定位置的读写关闭等操作。
并且从系统底层调用和标准c函数库两个角度对文件的操作比较中,认识到两者的异同4、程序清单//程序清单 3-1#include #include int main(void){char c;FILE *in,*out;if((in = fopen("file.in","r"))==NULL){ perror("file open error!"); exit(0);}out = fopen("file.out","w");while((c = fgetc(in))!=EOF)fputc(c,out);}//程序清单 3-2#include #include #include int main(){// char block[1024]; char c; int in, out; int nread; in = open("file.in", O_RDONLY); out = open("file.out", O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR);//将注释打开,并将两条语句的后一句注释掉,重新编译执行。
// while((nread = read(in,block,sizeof(block))) > 0) while((nread = read(in,&c,sizeof(c))) > 0)// write(out,block,nread); write(out,&c,nread);}//程序清单 3-3#include #include #include #include #include char buf1[] = "abcdefghij";char buf2[] = "ABCDEFGHIJ";void err_exit(char *err_s){ perror(err_s); exit(1);}int main(void){FILE *fp; if((fp = fopen("hole.file","w")) == NULL) err_exit("file open fail!"); if(fwrite(buf1,sizeof(buf1),1,fp)!=1) err_exit("file write buf1 error!"); if(fseek(fp,40,SEEK_SET)==-1) err_exit("fseek error!"); if(fwrite(buf2,strlen(buf2),1,fp)!=1) err_exit("file write buf2 error!"); fclose(fp);}//程序清单 3-4#include #include #include #include #include char buf1[] = "abcdefghij";char buf2[] = "ABCDEFGHIJ";void err_exit(char *err_s){ perror(err_s); exit(1);}int main(void){int fd; if((fd = open("hole.file",O_WRONLY|O_CREAT/*|O_APPEND,0644*/)) == -1) err_exit("file open fail!"); if(write(fd,buf1,10)!=10) err_exit("file write buf1 error!"); if(lseek(fd,40,SEEK_SET)==-1) err_exit("lseek error!"); if(write(fd,buf2,10)!=10) err_exit("file write buf2 error!");}实验四 Linux下进程操作与进程间通信1、实验目的 (1) 学习和掌握fork等系统调用的基本使用方法。
2) 利用Linux中的管道实现父子进程间的同步2、实验内容与步骤步骤1:使用Vi将程序清单4-1的程序输入、编译并运行,学习和掌握fork的基本调用方法步骤 2:使用Vi将程序清单4-2、4-3和4-4的程序输入、利用gcc分别编译这三个程序,写出编译命令和编译结果,如果不成功,尝试利用gdb调试gcc -o list4_2 list4_2.cgcc -o father father.cgcc -o child child.c步骤3:写出编译这三个程序的makefile,然后利用make进行编译,谈谈这么做的好处可以节省劳动时间,让三个程序同时编译,简化了操作步骤4:运行这三个程序,写出运行结果 步骤5:屏幕上显示出的执行结果是哪一个进程执行的结果?father和child步骤6:父进程中的printf有向屏幕输出吗?为什么?没有,因为父进程的标准输出已经被重定向步骤7:利用父子进程间的管道通信方式,改写实验3步骤6要求的程序要求启用两个进程,其中父进程接受用户对文件stu.info的操作命令然后通过管道发给子进程,子进程完成对文件的实际操作3、 实验结论 通过该节实验,我更进一步理解了fock函数dup函数exercl函数的工作原理,让我了解到了进程并发的概念。
4、程序清单//程序清单 4-1#include #include #include "err_exit.h"int global = 5;int main(void){ pid_t pid; char *string = "these are values before fork:"; int local = 10; printf("before fork * * *\n\n"); if((pid = fork())<0) err_exit("fork error"); if(pid == 0){ string = "I am child."; printf("\nMy pid is %d,%s\n" "pid = %d\n global = %d\n local = %d\n", getpid(),string,pid,global,local); global++; } else{ string = "I am parent."; printf("\nMy pid is %d,%s\n" "pid = %d\n global = %d\n local = %d\n", getpid(),string,pid,global,local); local++; } printf("%s\n Now,global = %d,local = %d\n",string,global,local); exit(EXIT_SUCCESS);}//清单4-2 管道程序#include #include#include #define STD_INPUT 0 // 定义标准输入设备描述符# define STD_OUTPUT 1 // 定义标准输出设备描述符int fd[2];void main(){static char process1[]="father",process2[]="child"; pipe(fd); // 定义管道 pipeline(process1,process2); // 调用自定义函数pipeline() exit(1); // 程序结束}pipeline(char* process1,char* process2){int i; if ((i=fork())==-1) // 创建进程,失败退出 { perror("process fork error!"); exit(1); } if (i) { close(fd[0]); // 关闭管道输入描述符 close(STD_OUTPUT); // 关闭标准输出描述符1 dup(fd[1]); // 指定标准输出描述符1为管道写指针 close(fd[1]); // 关闭原始管道写指针 execl(process1, process1, 0); // 用程序father覆盖当前程序 printf("father failed.\n"); // execl()执行失败 } else { close(fd[1]); // 关闭管道输出描述符 close(STD_INPUT); // 关闭标准输入描述符0 dup(fd[0]); // 指定标准输入描述符0为管道读指针 close(fd[0]); // 关闭原始管道读指针 execl(process2,process2,0); // 用程序child覆盖当前程序 printf("child failed.\n"); // execl()执行失败 } exit(2); // 程序结束}清单 4-3 father.c#include #include #include int main(){static char string[] = "Parent is using pipe write.";int len; len = sizeof(string); write(1, string, len); /* 将string中的内容写入管道中 */ printf("parent, parent, parent \n \n \n"); exit(0);}清单4-4 child.c#include #include int main(){ char output[30] ; read (0, output, 30) ; /* 从管道中读数据并存入output中 */ printf("%s \n child, child. \n", output) ; return(0) ;}实验五 Linux线程的使用1、实验目的 学习和掌握Linux线程的创建以及同步方法。
2、实验内容和步骤步骤1:输入程序5-1,编译并运行写出执行结果步骤 2:仔细研读代码,描述pthread_create函数中各个参数的意义,并给出线程的入口函数的调用方法,描述两线程间参数传递的方式int pthread_create(pthread_t *thread, const pthread_attr_t *attr,void *(*start_routine) (void *),void *arg);第一个参数为指向线程标识符的指针第二个参数用来设置线程属性第三个参数是线程运行函数的起始地址最后一个参数是运行函数的参数主线程通过pthread_create函数向从线程a_thread传递share_int 而从线程a_thread通过pthread_join向主线程传递thread_result步骤3:打开注释掉的全局变量aa及操作aa的相关代码,回答这跟进程有什么不同线程共用相同的数据区而各个进程有着自己独立的数据区步骤4:输入程序5-2,编译并运行,写出运行结果步骤5:仔细研读代码,描述利用信号量实现线程间同步调用方法sem_post 以原子操作的方式将信号量加1从而唤醒线程。
sem_wait以原子操作将信号量减1当信号量小于0时就会对进程进行等待,从而实现同步调用步骤6:学习并使用线程间的同步方法,重新改写实验3步骤6要求的程序要求启用两个线程,其中主线程接受用户对文件stu.info的操作命令然后发给子线程,子线程完成对文件的实际操作(注意线程间的同步)3、 实验结论通过改节实验,让我对线程并发有了初步认识,同时学会用信号量进行线程之间的同步4、程序清单//程序清单 5-1#include #include #include #include void *thread_function(void *arg);int main(){ int res; pthread_t a_thread; void *thread_result;int share_int = 10; res = pthread_create(&a_thread,NULL,thread_function,(void *)&share_int); sleep(5); share_int = -10; if(res != 0){ perror("Thread creation failed"); exit(EXIT_FAILURE); } printf("Waiting for thread to finish...\n"); res = pthread_join(a_thread,&thread_result); if(res != 0){ perror("Thread join failed"); exit(EXIT_FAILURE); } printf("Thread joined,it returned %s\n",(char *)thread_result); printf("share_int is now %d\n",share_int); exit(EXIT_SUCCESS);}void *thread_function(void *arg){ printf("thread_function is running.Argument was %d\n",*(int *)arg); *(int *)arg = *(int *)arg * 10; pthread_exit("Thank you for the CPU time");}//程序清单 5-2#include #include #include #include #include #include #include void *thread_function(void *arg);sem_t bin_sem;#define WORK_SIZE 1024char work_area[WORK_SIZE];int main(){ int res; pthread_t a_thread; void *thread_result; res = sem_init(&bin_sem,0,0); if (res != 0){ perror("Semaphore initialition failed"); exit(EXIT_FAILURE); } res = pthread_create(&a_thread,NULL,thread_function,NULL); if(res != 0){ perror("Thread creation failed"); exit(EXIT_FAILURE); } printf("Input some text,Enter 'end' to finish\n"); while(strncmp("end",work_area,3) != 0){ fgets(work_area,WORK_SIZE,stdin); sem_post(&bin_sem); } printf("\nWaiting for thread to finish...\n"); res = pthread_join(a_thread,&thread_result); if(res != 0){ perror("Thread join failed"); exit(EXIT_FAILURE); } printf("Thread joined\n"); sem_destroy(&bin_sem); exit(EXIT_SUCCESS);}void *thread_function(void *arg) { sem_wait(&bin_sem); while(strncmp("end",work_area,3) != 0){ printf("You input %d characters\n",strlen(work_area)-1); sem_wait(&bin_sem); } pthread_exit(NULL);}实验六 Linux进程间的IPC1、实验目的 学习和掌握Linux进程间的IPC及同步方法。
2、实验内容和步骤步骤1:输入程序6-1,6-2编译并运行写出执行结果步骤 2:仔细研读代码,写出程序中实现P、V操作的相关函数和代码,描述程序的同步工作过程P操作:if (!semaphore_p()) exit(EXIT_FAILURE); printf("%c", op_char); fflush(stdout); pause_time = rand() % 3; sleep(pause_time); printf("%c", op_char); fflush(stdout);V操作: if (!semaphore_v()) exit(EXIT_FAILURE); pause_time = rand() % 2; sleep(pause_time);P操作后操作当前的临界区占用资源,v操作退出当前的临界区,释放资源步骤3:输入程序6-3和6-4,编译执行并写出结果步骤4:程序6-3和6-4中哪段代码实现了共享,描述实现内存共享的主要函数的参数意义和这些函数的使用方法。
程序6_3:if((shm = shmat(shmid,NULL,0)) == (char *)-1) { perror("shmat error"); exit(0); } s = shm; for(c = 'a'; c <= 'z'; c++) *s++ = c; *s = NULL; printf("init over!\n"); while(*shm != '*') sleep(1);程序6_4:if((shm = shmat(shmid,NULL,0)) == (char *)-1){ perror("shmat error!"); exit(0); } for(s = shm; *s != NULL; s++) putchar(*s); putchar('\n'); *shm = '*';步骤5:学习并使用IPC中信号量和共享内存的使用方法,重新改写实验3步骤6要求的程序要求启动多个进程,每一个进程都可以单独对文件进行操作,进程间通过信号量进行同步,对文件的操作映射到共享内存中3、 实验结论通过对改节实验,让我了解了进程IPC信号量访问临界区,以及进程之间访问共享内存的实现方法和同步机制,掌握了上述一系列函数的使用方法。
4、程序清单//程序清单 6-1#if defined(__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED) /* union semun is defined by including */#else /* according to X/OPEN we have to define it ourselves */ union semun { int val; /* value for SETVAL */ struct semid_ds *buf; /* buffer for IPC_STAT, IPC_SET */ unsigned short int *array; /* array for GETALL, SETALL */ struct seminfo *__buf; /* buffer for IPC_INFO */ };#endif//程序清单 6-2#include #include #include #include #include #include #include "semun.h"static int set_semvalue(void);static void del_semvalue(void);static int semaphore_p(void);static int semaphore_v(void);static int sem_id;int main(int argc, char *argv[]){ int i; int pause_time; char op_char = 'O'; srand((unsigned int)getpid()); sem_id = semget((key_t)1234, 1, 0666 | IPC_CREAT); printf("semid = %d\n",sem_id); if (argc > 1) { if (!set_semvalue()) { fprintf(stderr, "Failed to initialize semaphore\n"); exit(EXIT_FAILURE); } op_char = 'X'; sleep(5); }/* Then we have a loop which enters and leaves the critical section ten times. There, we first make a call to semaphore_p which sets the semaphore to wait, as this program is about to enter the critical section. */ for(i = 0; i < 10; i++) { if (!semaphore_p()) exit(EXIT_FAILURE); printf("%c", op_char); fflush(stdout); pause_time = rand() % 3; sleep(pause_time); printf("%c", op_char); fflush(stdout);/* After the critical section, we call semaphore_v, setting the semaphore available, before going through the for loop again after a random wait. After the loop, the call to del_semvalue is made to clean up the code. */ if (!semaphore_v()) exit(EXIT_FAILURE); pause_time = rand() % 2; sleep(pause_time); } printf("\n%d - finished\n", getpid()); if (argc > 1) { sleep(10); del_semvalue(); } exit(EXIT_SUCCESS);}/* The function set_semvalue initializes the semaphore using the SETVAL command in a semctl call. We need to do this before we can use the semaphore. */static int set_semvalue(void){ union semun sem_union; sem_union.val = 1; if (semctl(sem_id, 0, SETVAL, sem_union) == -1) return(0); return(1);}/* The del_semvalue function has almost the same form, except the call to semctl uses the command IPC_RMID to remove the semaphore's ID. */static void del_semvalue(void){ union semun sem_union; if (semctl(sem_id, 0, IPC_RMID, sem_union) == -1) fprintf(stderr, "Failed to delete semaphore\n");}/* semaphore_p changes the semaphore by -1 (waiting). */static int semaphore_p(void){ struct sembuf sem_b; sem_b.sem_num = 0; sem_b.sem_op = -1; /* P() */ sem_b.sem_flg = SEM_UNDO; if (semop(sem_id, &sem_b, 1) == -1) { fprintf(stderr, "semaphore_p failed\n"); return(0); } return(1);}/* semaphore_v is similar except for setting the sem_op part of the sembuf structure to 1, so that the semaphore becomes available. */static int semaphore_v(void){ struct sembuf sem_b; sem_b.sem_num = 0; sem_b.sem_op = 1; /* V() */ sem_b.sem_flg = SEM_UNDO; if (semop(sem_id, &sem_b, 1) == -1) { fprintf(stderr, "semaphore_v failed\n"); return(0); } return(1);}//程序清单 6-3#include #include #include #include #include #include #define SHMSZ 27int main(void){ char c; int shmid; key_t key; char *shm,*s; key = 1234; if((shmid = shmget(key,SHMSZ,IPC_CREAT|0666))<0) { perror("shmget error"); exit(0); } if((shm = shmat(shmid,NULL,0)) == (char *)-1) { perror("shmat error"); exit(0); } s = shm; for(c = 'a'; c <= 'z'; c++) *s++ = c; *s = NULL; printf("init over!\n"); while(*shm != '*') sleep(1); exit(0);}//程序清单 6-4#include #include #include #include #include #include #define SHMSZ 27int main(void){ int shmid; key_t key; char *shm,*s; key = 1234; if((shmid = shmget(key,SHMSZ,0666)) < 0){ perror("shmget error!"); exit(0); } if((shm = shmat(shmid,NULL,0)) == (char *)-1){ perror("shmat error!"); exit(0); } for(s = shm; *s != NULL; s++) putchar(*s); putchar('\n'); *shm = '*'; exit(0);}实验七 Linux下访问Mysql数据库1、实验目的 学习和掌握Mysql数据库及编程访问方法。
2、实验内容和步骤步骤1:在Mysql中使用sql命令create database foo命令创建数据库foo步骤 2:在数据库foo中创建表childrenCreate table children { Childno int(11) default ‘0’not null auto_increment, Fname varchar(30), Age int(11), Primary key (childno));步骤3:向表children中插入若干记录Insert into children values (1,’Jenny’,17);Insert into children values (2,’Andrew’,13);Insert into children values (3,’Alex’,12);Insert into children values (4,’Tom’,11);Insert into children values (5,’Jack’,7);Insert into children values (6,’Ella’,14);步骤4:输入、编译并运行程序7-1、7-2和7-3,写出运行结果。
步骤5:学习并使用C语言访问Mysql的基本方法,重新改写实验3步骤6要求的程序要求使用数据库保存stu.info结构3、 实验结论通过改节实验我掌握了linux下怎么用c语言进行简单的数据库方面的编程4、程序清单//程序清单 7-1#include #include #include "mysql.h"。