1. 使用系统调用open read write,完成一个C语言程序:该程序接收两个参数,参数1作为要读写的文件,参数2作为标志位,标识是读还是写。功能完成对参数1所示文件的读写,该文件的内容为 “学号 MYFILE”,其中学号填为自己的学号。 2. 改造上面的程序,完成: i. 使用semaphore,并利用该程序生成2个进程,这两个进程写同一个文件,要求:a.互斥写,即只有一个进程写完后,才能让另一个进程写; b. 一个进程写入内容:“自己学号的后3位PROC1 MYFILE1”;另一个进程写入内容:“自己学号的后3位PROC2 MYFILE2 ii. 将上面程序的semaphore替换成strict alternation算法,完成上面两个进程的互斥。
时间: 2024-02-06 08:09:51 浏览: 72
1. 使用系统调用open read write,完成一个C语言程序:
```c
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[]) {
if(argc < 3) {
printf("Usage: ./file_rw filename flag\n");
exit(1);
}
char* filename = argv[1];
int flag = atoi(argv[2]);
int fd = open(filename, flag, 0666);
if(fd == -1) {
printf("Failed to open file: %s\n", filename);
exit(1);
}
char content[100];
sprintf(content, "学号 %s", "MYFILE");
if(flag == O_RDONLY) {
read(fd, content, strlen(content));
printf("%s\n", content);
} else if(flag == O_WRONLY) {
write(fd, content, strlen(content));
} else {
printf("Invalid flag!\n");
exit(1);
}
close(fd);
return 0;
}
```
2. 改造上面的程序,完成:
i. 使用semaphore,并利用该程序生成2个进程,这两个进程写同一个文件,要求:
a. 互斥写,即只有一个进程写完后,才能让另一个进程写;
b. 一个进程写入内容:“自己学号的后3位PROC1 MYFILE1”;另一个进程写入内容:“自己学号的后3位PROC2 MYFILE2”
```c
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <semaphore.h>
#define SEM_NAME "/my_semaphore"
#define FILENAME "test.txt"
int main() {
sem_t* semaphore = sem_open(SEM_NAME, O_CREAT, 0666, 1);
if(semaphore == SEM_FAILED) {
printf("Failed to open semaphore!\n");
exit(1);
}
pid_t pid1 = fork();
if(pid1 < 0) {
printf("Failed to create process 1!\n");
exit(1);
} else if(pid1 == 0) {
// Child process 1
sem_wait(semaphore);
int fd = open(FILENAME, O_WRONLY | O_APPEND, 0666);
if(fd == -1) {
printf("Failed to open file: %s\n", FILENAME);
exit(1);
}
char content[100];
sprintf(content, "%sPROC1 MYFILE1", "123456789");
write(fd, content, strlen(content));
close(fd);
sem_post(semaphore);
exit(0);
}
pid_t pid2 = fork();
if(pid2 < 0) {
printf("Failed to create process 2!\n");
exit(1);
} else if(pid2 == 0) {
// Child process 2
sem_wait(semaphore);
int fd = open(FILENAME, O_WRONLY | O_APPEND, 0666);
if(fd == -1) {
printf("Failed to open file: %s\n", FILENAME);
exit(1);
}
char content[100];
sprintf(content, "%sPROC2 MYFILE2", "123456789");
write(fd, content, strlen(content));
close(fd);
sem_post(semaphore);
exit(0);
}
// Wait for child processes to finish
waitpid(pid1, NULL, 0);
waitpid(pid2, NULL, 0);
sem_close(semaphore);
sem_unlink(SEM_NAME);
return 0;
}
```
ii. 将上面程序的semaphore替换成strict alternation算法,完成上面两个进程的互斥。
```c
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#define FILENAME "test.txt"
int main() {
pid_t pid1 = fork();
if(pid1 < 0) {
printf("Failed to create process 1!\n");
exit(1);
} else if(pid1 == 0) {
// Child process 1
int fd = open(FILENAME, O_WRONLY | O_APPEND, 0666);
if(fd == -1) {
printf("Failed to open file: %s\n", FILENAME);
exit(1);
}
char content[100];
sprintf(content, "%sPROC1 MYFILE1", "123456789");
while(1) {
lseek(fd, 0, SEEK_SET);
int lock = flock(fd, LOCK_EX);
if(lock == -1) {
printf("Failed to acquire lock!\n");
exit(1);
}
write(fd, content, strlen(content));
flock(fd, LOCK_UN);
sleep(1);
}
close(fd);
exit(0);
}
pid_t pid2 = fork();
if(pid2 < 0) {
printf("Failed to create process 2!\n");
exit(1);
} else if(pid2 == 0) {
// Child process 2
int fd = open(FILENAME, O_WRONLY | O_APPEND, 0666);
if(fd == -1) {
printf("Failed to open file: %s\n", FILENAME);
exit(1);
}
char content[100];
sprintf(content, "%sPROC2 MYFILE2", "123456789");
while(1) {
lseek(fd, 0, SEEK_SET);
int lock = flock(fd, LOCK_EX);
if(lock == -1) {
printf("Failed to acquire lock!\n");
exit(1);
}
write(fd, content, strlen(content));
flock(fd, LOCK_UN);
sleep(1);
}
close(fd);
exit(0);
}
// Wait for child processes to finish
waitpid(pid1, NULL, 0);
waitpid(pid2, NULL, 0);
return 0;
}
```
阅读全文