Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <signal.h>
- #include <sys/wait.h>
- pid_t pid, prevChild;
- int waiter, k = 0;
- void handleUSR1(int signum) {
- if(signum == SIGUSR1) {
- printf("<%d> am primit <semnal>\n", getpid());
- exit(1);
- }
- }
- void handleUSR2(int signum) {
- if(signum == SIGUSR2)
- waiter = 0;
- }
- void handleChildEnd(int signum) {
- int status;
- wait(&status);
- printf("Copilul <%d> s-a terminat\n", prevChild);
- waiter = 0;
- }
- int main(int argc, char** argv) {
- if(argc < 4) {
- printf("Numarul de argumente nu corespunde cerintei");
- exit(-1);
- }
- if(strlen(argv[2]) != strlen(argv[3])) {
- printf("String-urile nu au dimensiuni egale");
- exit(-2);
- }
- struct stat* stats = (struct stat*)malloc(sizeof(struct stat));
- if(!stats) {
- printf("Memoria pentru structura stat nu a fost apelata corespunzator");
- exit(-3);
- }
- int statFunction = stat(argv[1], stats);
- if(!S_ISREG(stats->st_mode)) {
- printf("Argumentul 1 nu este un fisier obsinuit");
- exit(-4);
- }
- free(stats);
- char* filePath = (char*)calloc(30, sizeof(char));
- if(!filePath) {
- printf("Memoria pentru filePath nu a fost alocata corespunzator");
- exit(-6);
- }
- strcpy(filePath, argv[1]);
- char newPath[30], name[30];
- strcpy(newPath, "");
- strcpy(name, argv[1]);
- name[strlen(name) - 4] = '\0';
- for(int i = 0; i < strlen(argv[2]); ++i) {
- waiter = 1;
- sprintf(newPath, "%s%d.txt", name, i);
- pid = fork();
- if(pid < 0) {
- perror("Fork: ");
- exit(-5);
- }
- if(!pid) {
- int fd1 = open(filePath, O_RDONLY);
- int fd2 = open(newPath, O_WRONLY | O_CREAT | O_TRUNC, 0777);
- struct sigaction sigUSR1;
- memset(&sigUSR1, 0x00, sizeof(struct sigaction));
- sigUSR1.sa_handler = handleUSR1;
- int signal = sigaction(SIGUSR1, &sigUSR1, NULL);
- if(signal < 0) {
- perror("Signal: ");
- exit(-9);
- }
- if(!fd1 || !fd2) {
- perror("Open: ");
- exit(-7);
- }
- int reader, writer;
- char* buffer = (char*)calloc(10, sizeof(char));
- if(!buffer) {
- printf("Memoria pentru buffer nu a fost alocata corespunzator");
- exit(-8);
- }
- while((reader = read(fd1, (void*)buffer, 10)) > 0) {
- for(int j = 0; j < reader; ++j)
- if(buffer[j] == argv[2][i])
- buffer[j] = argv[3][i];
- writer = write(fd2, buffer, reader);
- }
- sleep(1);
- close(fd1);
- close(fd2);
- free(buffer);
- if(i != 0)
- kill(prevChild, SIGUSR1);
- if(i == 0)
- kill(getppid(), SIGUSR2);
- if(i == strlen(argv[2]) - 1)
- exit(0);
- }
- strcpy(filePath, newPath);
- struct sigaction sigChild;
- memset(&sigChild, 0x00, sizeof(struct sigaction));
- sigChild.sa_handler = handleChildEnd;
- int signal1 = sigaction(SIGCHLD, &sigChild, NULL);
- struct sigaction sigUSR2;
- memset(&sigUSR2, 0x00, sizeof(struct sigaction));
- sigUSR2.sa_handler = handleUSR2;
- int signal2 = sigaction(SIGUSR2, &sigUSR2, NULL);
- if(signal1 < 0 || signal2 < 0) {
- perror("Signal: ");
- exit(-10);
- }
- while(waiter);
- prevChild = pid;
- sleep(1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement