Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <sys/wait.h>
- #include <stdlib.h>
- #include <signal.h>
- #include <time.h>
- #include <math.h>
- #include <sys/time.h>
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/msg.h>
- #include <time.h>
- #include <sys/shm.h>
- bool* mem;
- int id;
- void f() {
- mem[id] = 0;
- }
- int main(int argc, char* argv[]) {
- signal(SIGKILL, f);
- id = atoi(argv[1]);
- int delay = atoi(argv[2]);
- if (id == 0) {
- int shm_id = shmget(42, 3, IPC_CREAT | 0666);
- mem = (bool*)shmat(shm_id, null, 0);
- mem[0] = 0;
- mem[1] = 0;
- mem[2] = 0;
- } else {
- int shm_id = shmget(42, 3, 0666);
- mem = (bool*)shmat(shm_id, 0, 0);
- while (1) {
- sleep(delay);
- if (mem[0] || mem[1] || mem[2])
- printf("Файл занят\n");
- else {
- mem[id] = 1;
- printf("Запись в файл...\n");
- FILE* f = fopen("temp", "a");
- fprintf(f, "%d\n", id);
- sleep(1);
- fclose(f);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement