How to parse \n in a custom cli - c

I've written code for a shell that we call opsh (the OPen SHell).
Now my friend found an issue about backslash preceeded characters that don't behave as expected.
Example
opsh does not seem to pass on the -character to echo.
In opsh:
$
$echo "TEXT\n"
TEXTn
$
whereas in ksh:
$
$echo "TEXT\n"
TEXT
I don't know how to do it. In my code I just use a pipeline for the commands (you may look at the entire code in the repo) where the relevant code looks as follows
int run_cmd(char *cmd, bool background) {
char buffer[2];
buffer[0] = '|';
buffer[1] = '\0';
struct str_list *chunks = list_split(cmd, buffer);
struct pipeline *pipe = malloc(chunks->pipes * sizeof *pipe);
if (pipe == NULL) {
fprintf(stderr, "malloc failed!\n");
}
int i = 0;
for (i = 0; i < chunks->pipes; i++) {
pipe[i].data = malloc(sizeof(char **) * BUFFER_SIZE * chunks[i].size);
if (pipe[i].data == NULL) {
fprintf(stderr, "malloc failed!\n");
}
int j = 0;
pipe[i].size = chunks[i].size;
for (j = 0; j < chunks[i].size; j++) {
if (chunks[i].argv[j] == NULL) {
chunks[i].argv[j] = '\0';
break;
}
pipe[i].option = malloc(sizeof(int) * 10);
if (pipe[i].option == NULL) {
fprintf(stderr, "malloc failed!\n");
}
pipe[i].data[j] = strdup(chunks[i].argv[j]);
if (pipe[i].data[j] == NULL) {
perror("strdup");
exit(EXIT_FAILURE);
}
* pipe[i].option = * chunks[i].option;
}
pipe[i].data[j] = '\0';
}
int status = execute_pipeline(chunks->pipes, pipe, background);
return status;
}
int execute_pipeline(int n, struct pipeline *pipe, bool background) {
// background = false;
int status = 0;
pid_t pid = -2;
if (n > -1)
pid = fork();
if (pid < 0) {
perror("fork failed");
return -1;
}
/* If we are the child process, then go execute the string.*/
if (pid == 0) {
/* spawn(cmd);*/
fork_pipeline(n, pipe);
}
/*
* We are the parent process.
* Wait for the child to complete.
*/
if (!background) {
while (((pid = waitpid(pid, &status, 0)) < 0) && (errno == EINTR));
fprintf(stderr, "%d: executed\n", (int) pid);
}
if (pid < 0) {
fprintf(stderr, "Error from waitpid: %s", strerror(errno));
return -1;
}
if (WIFSIGNALED(status)) {
fprintf(stderr, "pid %ld: killed by signal %d\n",
(long) pid, WTERMSIG(status));
return -1;
}
return WEXITSTATUS(status);
}
/* Helper function that forks pipes */
void fork_pipeline(int n, struct pipeline *structpipeline) {
int i;
int in = 0;
int fd[2];
for (i = 0; i < n - 1; ++i) {
if (pipe(fd) == -1) {
err_syserr("Failed creating pipe");
}
spawn_pipe(in, fd[1], structpipeline + i);
close(fd[1]);
in = fd[0];
}
if (dup2(in, 0) < 0) {
err_syserr("dup2() failed on stdin for %s: ", structpipeline[i].data[0]);
}
if (strstr(structpipeline[i].data[0], "=")) {
/* do nothing for now, handle in parser instead... */
}
else {
fprintf(stderr, "%d: executing %s\n", (int) getpid(), structpipeline[i].data[0]);
//fprintf(stderr, "\n");
if (* (structpipeline[i].option) == 1) { /* output redirection */
int length = structpipeline[i].size;
char *filename = structpipeline->data[length - 1];
for (int k = length - 2; k < length; k++)
structpipeline->data[k] = '\0';
fd[1] = open(filename, O_WRONLY | O_CREAT, 0666);
dup2(fd[1], STDOUT_FILENO);
close(fd[1]);
} /* TODO: input redirection */
execvp(structpipeline[i].data[0], structpipeline[i].data);
err_syserr("failed to execute %s: ", structpipeline[i].data[0]);
}
}

Related

Trying to append to existing file in c in my own Unix Shell using >> not working

I'm trying to create my own unix shell and I've hit a wall while trying to append a command such as ls to an existing file, for example.
ls >> myOutput
I was able to do a basic redirection using > to print to an output file and figured doing >> would be quite similar, but I guess I'm wrong.
This is my code:
int pid;
int in = 0, out = 0, append = 0, j;
int fd0, fd1, fda;
char* args[MAX_ARGS];
char inFileName[64], outFileName[64];
//used to get arguments for desired command
//i.e. ls -a -l -t
get_args(cmdline, args);
//Commands used to exit the shell.
if(!strcmp(args[0], "quit") || !strcmp(args[0], "exit"))
{
exit(0);
}
pid = fork();
if(pid == 0)
{ /* child process */
for(j = 0; args[j] != '\0'; ++j)
{
if(strcmp(args[j], ">>") == 0)
{
args[j] = NULL;
strcpy(outFileName, args[j + 1]);
printf("You want to append data to existing file\n");
append = 2;
}
if(strcmp(args[j], "<") == 0)
{
args[j] = NULL;
strcpy(inFileName, args[j + 1]);
printf("input file name is %s\n", inFileName);
in = 2;
}
if(strcmp(args[j], ">") == 0)
{
args[j] = NULL;
strcpy(outFileName, args[j + 1]);
printf("output file name is %s\n", outFileName);
out = 2;
}
}
//printf("in is %d and out is %d\n", in, out);
if(!strcmp(args[0], "|"))
{
printf("You want to pipe info\n");
}
if(append)
{
**//here is where my issue occurs**
if((fda = open(outFileName, O_RDWR|O_APPEND)) < 0)
{
perror("Error appending data\n");
exit(0);
}
dup2(fda, STDOUT_FILENO);//1
close(fda);
}
if(in)
{
if((fd0 = open(inFileName, O_RDONLY, 0)) < 0)
{
perror("Couldn't read from input file\n");
exit(0);
}
//Changes where the command will read from STDIN to a input file.
dup2(fd0, 0);
close(fd0);
}
if(out)
{
if((fd1 = creat(outFileName , 0644)) < 0)
{
perror("Coudln't create output file\n");
exit(0);
}
dup2(fd1, STDOUT_FILENO);//1
close(fd1);
}
execvp(*args, args);
perror("exec failed");
exit(-1);
}
else if(pid > 0)
{ /* parent process */
waitpid(pid, NULL, 0);
}
else
{ /* error occurred */
perror("fork failed");
exit(1);
}
Please let me know if you would like further details. Any help would truly be appreciated.

Pipe Usage In C

I have a question on usage pipes in C. I want have a multidimensional array in main process. This array stores 24 lines of text and a line contains 256 byte (BUFFER). Here is my code.
BUFFER SIZE DEFINED ON TOP = 256 BYTE
READ_END DEFINED ON TOP = 0
WRITE END DEFINED ON TOP = 1
char buffer[BUFFER_SIZE];
char *read_msg[100];
char *write_msg[100];
FILE *fp;
fp = fopen(argv[1], "r");
pid_t pid;
int fd[2];
pid = fork();
if (pid < 0) { // error
fprintf(stderr, "Fork Failed");
return 1;
}
if (pid == 0) { // child process does the time consuming task
close(fd[WRITE_END]);
read(fd[READ_END], &read_msg, 24*BUFFER_SIZE);
close(fd[READ_END]);
for (int i = 0; i < 24; ++i) {
printf("Received string: %s\n", read_msg[i]);
}
}
if (pid > 0) { // parent is responsive immediately
for (int i = 0; i < 24; ++i) {
if (fgets(buffer, sizeof(buffer), fp)) {
write_msg[i] = malloc(sizeof(buffer) + 1);
strcpy(write_msg[i], buffer);
}
}
close(fd[READ_END]);
write(fd[WRITE_END], &write_msg, 24*BUFFER_SIZE);
close(fd[WRITE_END]);
}
}
}
In child process, i want to print these lines but it prints nothing. How can i fix it? Thanks...
SOLUTION
char buffer[BUFFER_SIZE];
char read_msg[100][BUFFER_SIZE];
char write_msg[100][BUFFER_SIZE];
FILE *fp;
fp = fopen(argv[1], "r");
pid_t pid;
int fd[2];
pipe(fd);
pid = fork();
if (pid < 0) { // error
fprintf(stderr, "Fork Failed");
return 1;
}
if (pid == 0) {
close(fd[WRITE_END]);
read(fd[READ_END], &read_msg, 24*BUFFER_SIZE);
close(fd[READ_END]);
for (int i = 0; i < 24; ++i) {
printf(read_msg[i]);
}
}
if (pid > 0) {
for (int i = 0; i < 24; ++i) {
if (fgets(buffer, sizeof(buffer), fp)) {
strcpy(write_msg[i], buffer);
}
}
close(fd[READ_END]);
write(fd[WRITE_END], &write_msg, 24*BUFFER_SIZE);
close(fd[WRITE_END]);
}
}
}
I forgot to write pipe(fd) before fork(). Thank you all :)

Pipes between parent and two children in C

I am trying to write a C program that uses pipes to send information between the parent and two children. The goal of the program is to achieve something similar to merge sort, for strings. I read the number of strings and then the Strings. The strings get divided between the 2 children, recursively until each child has only one string. I have to redirect the stdin of the child to read from the stdout of the parent.
For some reason none of the children read more than the first string.
How could I solve this problem?
int main(int argc, char * argv[]) {
int nrrows = 0;
char * buffer = NULL;
size_t n = 0;
getline(&buffer, &n, stdin);
char * endptr;
nrrows = strtol(buffer, &endptr, 10);
char rows[nrrows][MAX_LEN];
int i = 0;
n = 0;
while(i < nrrows) {
char * row = NULL;
getline(&row, &n, stdin);
strcpy(rows[i], row);
i++;
}
if(nrrows == 1) {
fprintf(stderr, "%s", rows[0]);
return 0;
}
int fdcp1[2];
int fdcp2[2];
if(pipe(fdcp1) < 0) {
fprintf(stderr, "pipe unsuccessfull\n");
return EXIT_FAILURE;
}
if(pipe(fdcp2) < 0) {
fprintf(stderr, "pipe unsuccessfull\n");
return EXIT_FAILURE;
}
pid_t chpid1 = fork();
if(chpid1 < 0) {
fprintf(stderr, "fork unsuccessfull\n");
return EXIT_FAILURE;
}
else if(chpid1 == 0) {
close(fdcp2[0]);
close(fdcp2[1]);
close(fdcp1[1]);
dup2(fdcp1[0], STDIN_FILENO);
execlp("./forksort", "child1", NULL);
}else {
close(fdcp1[0]);
dup2(fdcp1[1], STDOUT_FILENO);
double half = (nrrows / 2);
int h = half;
char b[2];
b[0] = '0' + h;
b[1] = '\n';
write(fdcp1[1], b, sizeof(b));
for(i = 0; i < h; i ++) {
rows[i][strlen(rows[i])] = '\0';
write(fdcp1[1], rows[i], sizeof(rows[i]));
}
pid_t chpid2 = fork();
if(chpid2 < 0) {
fprintf(stderr, "fork unsuccessfull\n");
return EXIT_FAILURE;
}else if(chpid2 == 0) {
close(fdcp1[0]);
close(fdcp1[1]);
close(fdcp2[1]);
dup2(fdcp2[0], STDIN_FILENO);
execlp("./forksort", "child2", NULL);
}else {
close(fdcp2[0]);
dup2(fdcp2[1], STDOUT_FILENO);
half = (nrrows / 2);
h = half;
char b[2];
b[0] = '0' + (nrrows - h);
b[1] = '\n';
write(fdcp2[1], b, sizeof(b));
for(i = h; i < nrrows; i ++) {
rows[i][strlen(rows[i])] = '\0';
write(fdcp2[1], rows[i], sizeof(rows[i]));
}
}
}
return 0;
}
It's bad news to modify a file descriptor that is associated with an open stream. I would account it highly likely to cause you trouble, and there is, moreover, no need to do that here. The parent should instead use fdopen() to open new streams on top of its ends of the pipes, and conduct I/O with its children via those instead of via the standard streams. In addition to being safer, that leaves the process's original standard streams available for it to communicate with its parent process.
With that approach, you could even stream the strings to be sorted back and forth among the processes, instead of redundantly buffering blocks of them in each process's memory. For instance, you might do something like this:
#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char * argv[]) {
char * buffer = NULL;
size_t buflen = 0;
int nrrows;
int fdpc1[2];
int fdcp1[2];
int fdpc2[2];
int fdcp2[2];
pid_t chpid1;
pid_t chpid2;
FILE *pipeout;
FILE *pipein1;
FILE *pipein2;
int half;
int i;
fprintf(stderr, "%s!!!!!!!!!!!!!!!!!\n", argv[0]);
getline(&buffer, &buflen, stdin);
fprintf(stderr, "number: %s from %s\n", buffer, argv[0]);
nrrows = strtol(buffer, NULL, 10);
if(nrrows <= 0) {
fprintf(stderr, "This is not a valid >0 number\n");
return EXIT_FAILURE;
} else if (nrrows == 1) {
/* ... read and echo back the one row ... */
getline(&buffer, &buflen, stdin);
fprintf(stderr, "%s", buffer);
return EXIT_SUCCESS;
}
/* There are at least two rows to sort */
if (pipe(fdcp1) < 0) {
fprintf(stderr, "pipe unsuccessfull\n");
return EXIT_FAILURE;
}
if (pipe(fdpc1) < 0) {
fprintf(stderr, "pipe unsuccessfull\n");
return EXIT_FAILURE;
}
chpid1 = fork();
if (chpid1 == 0) {
/* this is child process 1 */
close(fdcp1[1]);
close(fdpc1[0]);
dup2(fdcp1[0], STDIN_FILENO);
close(fdcp1[0]);
dup2(fdpc1[1], STDOUT_FILENO);
close(fdpc1[1]);
execlp("./forksort", "child1", NULL);
} else if (chpid1 < 0) {
fprintf(stderr, "fork unsuccessfull\n");
return EXIT_FAILURE;
}
/* this is the parent process */
close(fdcp1[0]);
close(fdpc1[1]);
if (pipe(fdcp2) < 0) {
fprintf(stderr, "pipe unsuccessfull\n");
return EXIT_FAILURE;
}
if (pipe(fdpc2) < 0) {
fprintf(stderr, "pipe unsuccessfull\n");
return EXIT_FAILURE;
}
chpid2 = fork();
if (chpid2 == 0) {
/* this is child process 2 */
close(fdcp1[1]);
close(fdpc1[0]);
close(fdcp2[1]);
close(fdpc2[0]);
dup2(fdcp2[0], STDIN_FILENO);
close(fdcp2[0]);
dup2(fdpc2[1], STDOUT_FILENO);
close(fdpc2[1]);
execlp("./forksort", "child2", NULL);
} else if (chpid2 < 0) {
fprintf(stderr, "fork unsuccessfull\n");
return EXIT_FAILURE;
}
/* this is the parent process */
close(fdcp2[0]);
close(fdpc2[1]);
/* copy the first half of the lines from input to child 1 */
pipeout = fdopen(fdcp1[1], "w");
if (pipeout == NULL) {
fprintf(stderr, "fdopen unsuccessful\n");
return EXIT_FAILURE;
}
half = nrrows / 2;
fprintf(pipeout, "%d\n", half);
for (i = 0; i < half; i += 1) {
getline(&buffer, &buflen, stdin);
fprintf(stderr,"row[%d] from %s: %s", i, argv[0], buffer);
fputs(buffer, pipeout);
}
fclose(pipeout);
/* copy the second half of the lines from input to child 2 */
pipeout = fdopen(fdcp2[1], "w");
if (pipeout == NULL) {
fprintf(stderr, "fdopen unsuccessful\n");
return EXIT_FAILURE;
}
fprintf(pipeout, "%d\n", nrrows - half);
for (; i < nrrows; i += 1) {
getline(&buffer, &buflen, stdin);
fprintf(stderr,"row[%d] from %s: %s", i, argv[0], buffer);
fputs(buffer, pipeout);
}
fclose(pipeout);
/* now read and merge sorted lines from the children */
pipein1 = fdopen(fdpc1[0], "r");
pipein2 = fdopen(fdpc2[0], "r");
if (pipein1 == NULL || pipein2 == NULL) {
fprintf(stderr, "fdopen unsuccessful\n");
return EXIT_FAILURE;
}
/* ... */
fclose(pipein1);
fclose(pipein2);
return 0;
}

Pipes not working while implementing a minishell

I'm actually trying to implement a basic minishell in C. For doing that, I made a fonction which parse what the user enter in the console. I parsed it and then I would like to send the command to the console using pipes. I don't understand why my pipes are not working.
I checked the parsing and it seems to be fine, i have the right commands in parameters of the fonction.
The thing is that my pipe code is literally doing nothing and I don't understand why.
Here is my code.
Thank you in advance.
#define READ 0
#define WRITE 1
char *cmds[5] = {0};
int main() {
char *saisie = (char*)malloc(100*sizeof(char*));
char *saisie2 = (char*)malloc(100*sizeof(char*));
gets(saisie);
int ncmds = 0;
int k = 0;
char* token = (char*)malloc(100*sizeof(char*));
char* tofree;
if(*(saisie + 0) == '$'){
if(*(saisie + 2) == 'e' && *(saisie + 3) == 'x' && *(saisie + 4) == 'i' || *(saisie + 5) == 't'){
exit(0);
}
else{
int i;
for(i = 0;i<99;i++){
*(saisie2+i) = *(saisie+i+1);
}
free(saisie);
if (saisie2 != NULL) {
tofree = saisie2;
while ((token = strsep(&saisie2, "|")) != NULL){
cmds[ncmds] = token;
ncmds++;
}
free(tofree);
}
}
}
exe(cmds, ncmds);
while(wait(NULL) > 0);
return 0;
}
int exe(char *cmds[], int ncmds){
int fdin, fdout;
int fds[2];
int i;
int status;
fdin = 0;
for(i=0; i < ncmds-1; i++){
pipe(fds);
fdout = fds[WRITE];
if(fork() == 0){
if( fdin != 0 ) {
close(0);
dup(fdin);
close(fdin);
}
if( fdout != 1 ) {
close(1);
dup(fdout);
close(fdout);
}
close(fds[READ]);
const char* prog2[] = {cmds[i], "-l", 0};
execvp(cmds[i], prog2);
fprintf(stderr, "si esto se ve es un error\n");
exit(1);
}
if(fdin != 0)
close(fdin);
if(fdout != 1)
close(fdout);
fdin = fds[READ];
}
/* Ultimo comando */
fdout = 1;
if(fork() == 0) {
if( fdin != 0 ) {
close(0);
dup(fdin);
close(fdin);
}
const char* prog2[] = {cmds[i], "-l", 0};
execvp(cmds[i], prog2);
close(fds[READ]);
exit(1);
}
if(fdout!= 1)
close(fdout);
if(fdin != 0)
close(fdin);
}
}
int exe(char *cmds[], int ncmds){
int p2c[2];//pipe parent to child
int c2p[2];//pipe child to parent
int i;
int status;
int pid;
char buf[4096];
memset(buf, 0, 4096);
for(i=0; i < ncmds; i++){
pipe(p2c);
pipe(c2p);
pid = fork();
if(pid < 0) {
exit 1;
}
if(pid == 0){ //in child
close(1);
dup2(c2p[1],1); // make child write to c2p pipe instead of stdout
close(0);
dup2(p2c[0],0); // make child read from p2c pipe instead of stdin
close(p2c[1]);
close(c2p[0]);
const char* prog2[] = {cmds[i], "-l", 0};
execvp(cmds[i], prog2);
fprintf(stderr, "si esto se ve es un error\n");
exit(1);
}
//in parent
write(p2c[1], buf, strlen(buf)); //write the last output to child
close(p2c[1]);
close(c2p[1]);
memset(buf,0,4096);
if(read(c2p[0], buf, 4096) > 0){ //read output from child
if(i == ncmds -1 ){
printf("result:\n");
printf("%s\n", buf);
}
}
}
}

C language: Children don't exit, nor terminate and are not killed my parent process too

I can't exit or terminate children processes sending a signal.
Could you please tell me what I'm doing wrong in this code:
//###################################### INVERTER.C (main)
#include <semaphore.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <math.h>
#include <errno.h>
#include <unistd.h>
#include "timeprofiler.h"
#include "ppmtools.h"
//Global vars:
int shmids[4], shmPixelId, *total_lines, *processed_lines, *next_line, *buf_vars;
//To share unnamed semaphores between processes, they must be allocated in a shared memory.
mem_struct *sh_mm;
//unnamed semaphores
sem_t *mutex1, *mutex2, *mutex3, *sem_remaining_lines;
//struct that will hold the image in shared memory
image_struct *image;
pid_t *workersPID;
header *h;
int main(int argc, char *argv[]) {
int i, j, k, cur = 0, id;
pixel *row;
double start, stop, startms, stopms;
if (argc < 3) {
printf("Incorrect usage.\nPlease use \"./invert input_filename.ppm output_filename.ppm\"\n");
return -1;
}
//BLOCK ALL SIGNAL
sigset_t block_ctrlc;
sigfillset(&block_ctrlc);
sigdelset(&block_ctrlc, SIGINT);
sigprocmask(SIG_BLOCK, &block_ctrlc, NULL);
//start timer
start = getCurrentTimeMicro();
startms = getCurrentTimeMili();
printf("Opening input file [%s]\n", argv[1]);
FILE *fpin = fopen(argv[1], "r");
if (fpin == NULL) {
printf("Could not open input file\n");
return -1;
}
printf("Opening output file [%s]\n", argv[2]);
FILE *fpout = fopen(argv[2], "w");
if (fpout == NULL) {
printf("Could not open output file\n");
return -1;
}
printf("Getting header\n");
h = getImageHeader(fpin);
if (h == NULL) {
printf("Error getting header from file\n");
return -1;
}
printf("Got file Header: %s - %u x %u - %u\n", h->type, h->width, h->height, h->depth);
printf("Saving header to output file\n");
if (writeImageHeader(h, fpout) == -1) {
printf("Could not write to output file\n");
return -1;
}
init();
printf("After init...\n");
//alloc mem space for one row (width * size of one pixel struct)
row = (pixel *) malloc(h->width * sizeof (pixel));
printf("Starting work\n");
for (i = 0; i < h->height; i++) {
printf("Reading row... \n");
if (getImageRow(h->width, row, fpin) == -1) {
printf("Error while reading row\n");
}
printf("Got row %d || \n", (i + 1));
for (j = cur, k = 0; j < cur + h->width; j++, k++) {
image->pixel_data[j].red = row[k].red;
image->pixel_data[j].blue = row[k].blue;
image->pixel_data[j].green = row[k].green;
}
cur += h->width;
}
/*Creates workers*/
workersPID = (pid_t*) malloc(sizeof (pid_t) *((NUM_WORKERS)));
for (i = 0; i < NUM_WORKERS; i++) {
id = fork();
if (id == -1) {
printf("Error creating worker no %d\n", i);
return (EXIT_FAILURE);
} else if (id == 0) {
workersPID[i] = getpid();
printf("Launching son with pid %d\n", getpid());
worker(i);
}
}
cur = 0;
sem_wait(mutex2);
/*Writes the invert image on the output file*/
for (i = 0; i < h->height; i++) {
for (j = cur, k = 0; j < cur + h->width; j++, k++) {
row[k].red = image->pixel_data[j].red;
row[k].blue = image->pixel_data[j].blue;
row[k].green = image->pixel_data[j].green;
}
cur += h->width;
printf("Saving row... \n");
if (writeRow(h->width, row, fpout) == -1) {
printf("Error while writing row\n");
}
printf("Done\n");
}
printf("Cleaning up...\n");
//clean up row
free(row);
//clean up header
free(h);
printf("Closing file pointers.\n");
fclose(fpin);
fclose(fpout);
//stop timer
stop = getCurrentTimeMicro();
stopms = getCurrentTimeMili();
for (i = 0; i < NUM_WORKERS; i++) {
if (workersPID[i]) {
kill(workersPID[i], SIGTERM);
waitpid(workersPID[i], NULL, 0);
}
}
terminate();
printTimeElapsed(start, stop, "microseconds");
printTimeElapsed(startms, stopms, "miliseconds");
printf("Done!\n");
return 0;
}
void init() {
//create shared memory to hold the source image:
if ((shmids[0] = shmget(IPC_PRIVATE, sizeof (image_struct), IPC_CREAT | 0700)) == -1) {
printf("shmget to allocate image struct failed. Errno returned: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
image = (image_struct*) shmat(shmids[0], NULL, 0);
//shared memory to allocate the pointer to pointer pixel_data
if ((shmids[1] = shmget(IPC_PRIVATE, h->width * h->height * sizeof (pixel), IPC_CREAT | 0700)) == -1) {
printf("shmget to allocate pixel_data array failed. Errno returned: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
image->pixel_data = (pixel*) shmat(shmids[1], NULL, 0);
/*Shared Memory segment for 3 integers*/
if ((shmids[2] = shmget(IPC_PRIVATE, 3 * sizeof (int), IPC_CREAT | 0700)) == -1) {
printf("shmget to allocate the 3 integers failed. Errno returned; %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
buf_vars = (int*) shmat(shmids[2], NULL, 0);
total_lines = &buf_vars[0];
processed_lines = &buf_vars[1];
next_line = &buf_vars[2];
*processed_lines = *next_line = 0;
*total_lines = h->height;
if ((shmids[3] = shmget(IPC_PRIVATE, sizeof (mem_struct), IPC_CREAT | 0700)) == -1) {
printf("shmget to allocate mem_Struct for semaphores failed. Errno returned %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
sh_mm = (mem_struct*) shmat(shmids[3], NULL, 0);
if (sem_init(&sh_mm->mutex1, 1, 1) == -1) {
printf("Error initializing semaphore mutex1.Errno returned: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
mutex1 = &sh_mm->mutex1;
if (sem_init(&sh_mm->mutex2, 1, 0) == -1) {
printf("Error initializing semaphore mutex2.Errno returned: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
mutex2 = &sh_mm->mutex2;
if (sem_init(&sh_mm->mutex3, 1, 1) == -1) {
printf("Error initializing semaphore mutex3.Errno returned: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
mutex3 = &sh_mm->mutex3;
if (sem_init(&sh_mm->sem_remaining_lines, 1, h->height) == -1) {
printf("Error initializing semaphore sem_remaining_lines.Errno returned: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
sem_remaining_lines = &sh_mm->sem_remaining_lines;
}
/*Worker process*/
void worker(int id) {
int i, k, cur = 0;
pixel *row;
//Block all signals, except SIGINT and SIGKILL which are handled
sigset_t block_ctrlc;
sigfillset(&block_ctrlc);
sigdelset(&block_ctrlc, SIGINT);
sigdelset(&block_ctrlc, SIGTERM);
sigprocmask(SIG_BLOCK, &block_ctrlc, NULL);
signal(SIGINT, handle_signal);
signal(SIGTERM, handle_signal);
while (sem_wait(sem_remaining_lines)!= -1) { //if there are still lines to read, go on
sem_wait(mutex3);
cur = *next_line; //current image's line
*next_line += h->width; //refreshs line for the next worker
sem_post(mutex3);
row = (pixel *) malloc(h->width * sizeof (pixel));
for (i = cur, k = 0; i < cur + h->width; i++, k++) {
row[k].red = image->pixel_data[i].red;
row[k].blue = image->pixel_data[i].blue;
row[k].green = image->pixel_data[i].green;
}
//printf("% - Inverting row... \n",id);
invertRow(h->width, row); //invert
//printf("Done || \n");
for (i = cur, k = 0; i < cur + h->width; i++, k++) {
image->pixel_data[i].red = row[k].red;
image->pixel_data[i].blue = row[k].blue;
image->pixel_data[i].green = row[k].green;
}
sem_wait(mutex1);
*processed_lines += 1; //increases the number of inverted lines
if (*processed_lines == *total_lines) { //check if it reaches last line
sem_post(mutex2); //if so, wakes the master telling that is ready
}
sem_post(mutex1);
}
//printf("Son %d is exiting\n",id);
exit(0);
}
void handle_signal(int signum) {
if(signum == SIGINT)
signal(SIGINT, handle_signal);
else
signal(SIGTERM, handle_signal);
exit(0);
}
void terminate() {
int i;
//close semaphores
sem_destroy(mutex1);
sem_destroy(mutex2);
sem_destroy(mutex3);
sem_destroy(sem_remaining_lines);
//cleans up shared memory = removes shared memory segments
for (i = 0; i < 4; i++) {
shmctl(shmids[i], IPC_RMID, NULL);
}
}
I'm gonna leave the explanation of the assignment (that has already finished btw)here:
1 page pdf
Your worker threads have SIGTERM blocked (because it was blocked in main, and sigprocmask doesn't remove signals from the blocked set unless explicitly told to do so)
You may want to do something like this in the worker instead:
sigemptyset(&block_ctrlc);
sigaddset(&block_ctrlc, SIGINT);
sigaddset(&block_ctrlc, SIGTERM);
sigprocmask(SIG_UNBLOCK, &block_ctrlc, NULL);
Alternately, call sigprocmask with SIG_SETMASK.

Resources