This code takes in input N-file names and generates N-threads, then the main thread writes something and puts it in a buffer for each thread, and each threads writes their buffer content on their own file. Everything works fine but on the file, the input is like this and i don't know why, hope someone can help me, thanks in advance
Hello����������������������������������������������������������������������������������������������������������������������������
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/mman.h>
#include <semaphore.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define fflush(stdin) while (getchar() != '\n')
int n_threads;
FILE **source_files;
char **files;
char buffers[128][128];
pthread_mutex_t *ready;
pthread_mutex_t *done;
char buff[256];
void *thread_function(void *);
int main(int argc, char **argv) {
long i, j, ret;
pthread_t tid;
if (argc < 2) {
printf("Error: Usage -> prog | file1 ... fileN\n");
exit(1);
}
n_threads = argc - 1;
files = argv + 1;
for(i = 0; i < n_threads; i++) {
for(j = 0; j < n_threads; j++) {
if(strcmp(files[i], files[j]) == 0 && i != j) {
puts("Strings are equal.\n");
exit(1);
}
}
}
puts("Strings are different");
/*buffers = (char **)malloc(sizeof(char *) * n_threads);
if(buffers == NULL) {
printf("Malloc error.\n");
exit(1);
}*/
source_files = (FILE **)malloc(sizeof(FILE *) * n_threads);
if(source_files == NULL) {
printf("Malloc error.\n");
exit(1);
}
ready = malloc(sizeof(pthread_mutex_t));
done = malloc(sizeof(pthread_mutex_t));
if (ready == NULL || done == NULL) {
printf("Malloc error.\n");
exit(1);
}
if(pthread_mutex_init(ready, NULL) || pthread_mutex_init(done, NULL) || pthread_mutex_lock(ready)) {
printf("Mutex init error.\n");
exit(1);
}
for(i = 0; i < n_threads; i++) {
if(pthread_create(&tid, NULL, thread_function, (void *) i)) {
printf("Error while creating thread %ld.\n", i);
exit(1);
}
}
//signal(SIG_INT, printer);
i = 0;
while(1) {
if(pthread_mutex_lock(done)) {
printf("Mutex lock error.\n");
exit(1);
}
ret = scanf(" %s", buff);
if(ret == EOF) {
printf("Scanf error.\n");
exit(1);
}
if (ret == 0) {
printf("Non compliant input.\n");
exit(1);
}
getchar();
puts("before strcpy\n");
strcpy(buffers[i],buff);
puts("after strcpy\n");
if(pthread_mutex_unlock(ready)) {
printf("Mutex unlock error.\n");
exit(1);
}
i=(i+1)%n_threads;
}
}
void *thread_function (void *arg) {
int fd;
//FILE *file;
long me = (long) arg;
printf("Thread %ld started.\n", me);
puts("before opening file.\n");
fd = open(files[me], O_CREAT | O_RDWR | O_TRUNC, 0666);
//file = fdopen(fd, "w+");
puts("after opening file.\n");
while(1) {
if(pthread_mutex_lock(ready)) {
printf("Mutex lock error.\n");
exit(1);
}
printf("Thread %ld took string %s.\n", me, buffers[me]);
puts("before fprintf.\n");
//fprintf(file, "%s", buffers[me]);
write(fd, buffers[me], 128);
puts("after fprintf.\n");
if(pthread_mutex_unlock(done)) {
printf("Mutex unlock error.\n");
exit(1);
}
}
}
Related
I'm writing a program in C using inter-process communication, specifically I'm trying to write a program using an inter-process message queue. The program should work like this:
The command line accepts n-files (at least one). N-processes will be created as much as files.
The n processes must send the contents of the file to a process called Receiver which will have the task of printing the messages received.
The problem is: not all the contents of the file are printed, even if the processes send the messages. Why ? Could anyone tell me where I'm going wrong?
This is my code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/stat.h>
#define DIM_MSG 1024
#define TYPE_W 2
typedef struct
{
long mtype;
char eof;
char mtext[DIM_MSG];
} msg;
void child_r(int coda, const char *file)
{
FILE *r_stream;
if ((r_stream = fopen(file, "r")) == NULL)
{
perror("errore apertura file");
exit(1);
}
printf("%s:\n",file);
msg messaggio;
while (fgets(messaggio.mtext, DIM_MSG, r_stream) != NULL)
{
messaggio.mtype = TYPE_W;
messaggio.eof = 0;
if (msgsnd(coda, &messaggio, sizeof(msg) - sizeof(long) , 0) == -1)
{
perror("msgsnd");
exit(1);
}
printf("\tMessage send: %s", messaggio.mtext);
}
strcpy(messaggio.mtext, "quit");
messaggio.eof = 1;
messaggio.mtype = TYPE_W;
if (msgsnd(coda, &messaggio, sizeof(msg) - sizeof(long) , 0) == -1)
{
perror("msgsnd");
exit(1);
}
fclose(r_stream);
exit(0);
}
void child_f(int coda)
{
msg messaggio;
printf("\nReceiver\n");
do
{
if (msgrcv(coda, &messaggio, sizeof(msg) - sizeof(long), TYPE_W, 0) == -1)
{
perror("msgrcv");
exit(1);
}
if (strcmp(messaggio.mtext, "quit") != 0)
{
printf("\tMessage rcv: %s ", messaggio.mtext);
}
} while (messaggio.eof != 1);
exit(0);
}
int main(int argc, char const *argv[])
{
char *file_name = NULL;
struct stat sb;
int child=0;
int ds_coda;
if(argc<1)
{
fprintf(stderr,"Utilizzo %s <file-1> <file-2> <file-n>....",argv[0]);
exit(1);
}
if((ds_coda=msgget(IPC_PRIVATE,IPC_CREAT|IPC_EXCL|0600))==-1)
{
perror("coda");
exit(1);
}
/* analizza la command-line */
for (int i = 1; i < argc; i++) {
if ((stat(argv[i], &sb) == 0) && (S_ISREG(sb.st_mode)))
{
file_name = (char*)argv[i];
child++;
if(fork()==0)
{
child_r(ds_coda, file_name);
}
}
else {
perror(argv[i]);
exit(1);
}
}
if(child==0)
{
fprintf(stderr,"Parametri non validi!\n");
exit(1);
}
if(fork() == 0)
{
// child_w
sleep(1);
child_f(ds_coda);
}
else wait(NULL);
msgctl(ds_coda, IPC_RMID, NULL);
return 0;
}
A few issues ...
The main process needs to loop on wait before doing IPC_RMID. Otherwise, the sender processes will fail on msgsnd because the ds_coda is no longer valid. The main process has "raced" with the sender/receiver processes and removed the id with IPC_RMID before the other processes have completed.
All senders will set messsaggio.eof but the receiver stops after receiving the first one. It must know how many senders there are and wait until all have sent EOF. (i.e.) It must maintain a count.
Before I could debug this, I had to enhance the logging. So, I created tscgetf, logopen, and logprt to create separate logs for each process with timestamps.
When I was getting close, I added the from field to the message because the receiver was getting data but didn't know which process sent it. This helped diagnose the EOF issue.
Here is the refactored code. It is annotated with the bugs and fixes.
By default, it will show the EOF issue (i.e. it will hang). To apply the fix for the EOF issue, compile with -DFIXEOF
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <time.h>
#define DIM_MSG 1024
#define TYPE_W 2
typedef struct {
long mtype;
int from;
char eof;
char mtext[DIM_MSG];
} msg;
int pididx; // linear process ID (0=main)
int child = 0; // number of spawned children
int ds_coda;
FILE *xflog; // debug log stream
#define logprt(_fmt...) \
do { \
fprintf(xflog,"[%.9f/%5.5d] ",tscgetf(),pididx); \
fprintf(xflog,_fmt); \
fflush(xflog); \
} while (0)
double tsczero;
// tscget -- get timestamp
// RETURNS: elapsed seconds
double
tscgetf(void)
{
struct timespec ts;
double sec;
clock_gettime(CLOCK_MONOTONIC,&ts);
sec = ts.tv_nsec;
sec /= 1e9;
sec += ts.tv_sec;
sec -= tsczero;
return sec;
}
void
logopen(void)
{
char logf[100];
if (xflog != NULL)
fclose(xflog);
sprintf(logf,"log%3.3d",pididx);
xflog = fopen(logf,"w");
if (xflog == NULL) {
perror(logf);
exit(1);
}
}
void
child_r(int coda, const char *file)
{
FILE *r_stream;
logopen();
if ((r_stream = fopen(file, "r")) == NULL) {
perror("errore apertura file");
exit(1);
}
logprt("child_r: reading %s\n", file);
msg messaggio;
messaggio.from = pididx;
while (fgets(messaggio.mtext, DIM_MSG, r_stream) != NULL) {
messaggio.mtype = TYPE_W;
messaggio.eof = 0;
if (msgsnd(coda, &messaggio, sizeof(msg) - sizeof(long), 0) == -1) {
perror("msgsnd");
exit(1);
}
logprt("Message send: %s", messaggio.mtext);
}
strcpy(messaggio.mtext, "quit");
messaggio.eof = 1;
messaggio.mtype = TYPE_W;
#if 1
strcpy(messaggio.mtext,"I_AM_EOF\n");
#endif
if (msgsnd(coda, &messaggio, sizeof(msg) - sizeof(long), 0) == -1) {
perror("msgsnd");
exit(1);
}
logprt("Message EOF: %s", messaggio.mtext);
fclose(r_stream);
logprt("child_r: finished %s\n", file);
fclose(xflog);
exit(0);
}
void
child_f(int coda)
{
msg messaggio;
logopen();
// NOTE: we are started last so the count we need to wait for is one less
int waitcnt = pididx - 1;
logprt("Receiver starting -- waitcnt=%d\n",waitcnt);
while (1) {
if (msgrcv(coda, &messaggio, sizeof(msg) - sizeof(long), TYPE_W, 0) == -1) {
perror("msgrcv");
exit(1);
}
if (strcmp(messaggio.mtext, "quit") != 0) {
logprt("Message rcv (from %d): %s",
messaggio.from, messaggio.mtext);
}
if (messaggio.eof == 1) {
// NOTE/BUG: we can't stop after the first EOF message -- we must wait for all
// of them
#if ! FIXEOF
logprt("got EOF\n");
break;
#else
logprt("got EOF -- waitcnt=%d\n",waitcnt);
if (--waitcnt <= 0)
break;
#endif
}
}
logprt("child_f: complete\n");
fclose(xflog);
exit(0);
}
// start_rcv -- start receiver process
void
start_rcv(void)
{
logprt("start_rcv:\n");
child++;
pid_t pid = fork();
if (pid == 0) {
pididx = child;
// child_w
// NOTE/BUG: not necessary with other fixes
#if 0
sleep(1);
#endif
child_f(ds_coda);
}
logprt("start_rcv: pid=%d child=%d\n",pid,child);
}
int
main(int argc, char const *argv[])
{
char *file_name = NULL;
struct stat sb;
tsczero = tscgetf();
#if 1
pid_t pid;
setlinebuf(stdout);
setlinebuf(stderr);
#endif
logopen();
if (argc < 1) {
fprintf(stderr, "Utilizzo %s <file-1> <file-2> <file-n>....", argv[0]);
exit(1);
}
if ((ds_coda = msgget(IPC_PRIVATE, IPC_CREAT | IPC_EXCL | 0600)) == -1) {
perror("coda");
exit(1);
}
// NOTE: early attempt to fix (receiver should start first) but didn't fix it
// and won't work because receiver needs to know the number of EOF messages to
// wait for
#if RCVEARLY
start_rcv();
#endif
/* analizza la command-line */
for (int i = 1; i < argc; i++) {
if ((stat(argv[i], &sb) == 0) && (S_ISREG(sb.st_mode))) {
file_name = (char *) argv[i];
child++;
pid = fork();
if (pid == 0) {
pididx = child;
child_r(ds_coda, file_name);
}
#if 1
else {
logprt("forked: pid=%d child=%d\n",pid,child);
}
#endif
}
else {
perror(argv[i]);
exit(1);
}
}
if (child == 0) {
fprintf(stderr, "Parametri non validi!\n");
exit(1);
}
// NOTE/FIX: main process must wait for _all_ children to complete before
// doing IPC_RMID
#if 1
#if ! RCVEARLY
start_rcv();
#endif
while (1) {
pid_t pid = wait(NULL);
logprt("waitfor: %d child=%d\n",pid,child);
if (pid <= 0)
break;
--child;
}
#endif
msgctl(ds_coda, IPC_RMID, NULL);
fclose(xflog);
return 0;
}
In the code above, I've used cpp conditionals to denote old vs. new code:
#if 0
// old code
#else
// new code
#endif
#if 1
// new code
#endif
Note: this can be cleaned up by running the file through unifdef -k
I'm working with two programs. The customer.c program writes an int to a named pipe and the bank.c reads the pipe and prints the int. The customer will choose one of two named pipes, "atm1" and "atm2".
Ultimately I would like to run two customer.c programs, one for each pipe at the same time, but I have some issues with the writing and reading to the named pipe.
If I just run bank.c and one customer.c I don't get any output.
If I run bank.c and two customer.c the output doesn't always print or is out of order when it does.
I tried to use fsync() to flush but that didn't work either.
customer.c
int main(int argc, char *argv[]){
int fd, num =0;
if((fd = open(argv[1], O_WRONLY)) == -1){
...
}
while(1){
printf("Enter a integer:\n");
scanf("%d", &num);
if(num < 0){
break;
}
if(write(fd, &num, sizeof(num)) <= 0){...}
fsync(fd);
}
close(fd);
return 0;
}
bank.c
int main(){
int fd, sd, num=0, sret, fret, maxfd;
//fd_set readfds;
//struct timeval timeout;
if(mkfifo("atm1", 0666) == -1){...}
if(mkfifo("atm2", 0666) == -1){...}
if((fd = open("atm1", O_RDONLY)) == -1){...}
if((sd = open("atm2", O_RDONLY)) == -1){...}
while(1){
if((sret = read(sd, &num, sizeof(num))) > 0){
printf("%d\n", num);
}
if((fret = read(fd, &num, sizeof(num))) > 0){
printf("%d\n", num);
}
if(sret <= 0 && fret <= 0){
break;
}
}
close(fd);
close(sd);
return 0;
}
Any pointer?
you need to make multi-threads server for multiple same time recv. and require to check pipe name is already exist.
blank.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <pthread.h>
void *client(void *data)
{
char *pname = (char*) data ;
int sd =0 ;
int num ;
int sret=0 ;
if((sd = open(pname, O_RDONLY)) == -1) {
printf("open failed:%s\n", pname) ;
return NULL ;
}
printf("client[%d] start\n", sd) ;
while(1){
num=0 ;
if((sret = read(sd, &num, sizeof(num))) > 0){
printf("[%d] recv:%d\n", sd, num);
}
if(sret <= 0) {
break;
}
}
close(sd);
printf("client[%d] end\n", sd) ;
return NULL;
}
int main(){
int status ;
pthread_t t1, t2 ;
if(mkfifo("atm1", 0666) == -1) {
printf("mkfifio failed : %d\n", errno) ;
if (errno==EEXIST ) {
printf("already exist. ignore\n") ;
}
else
return -1;
}
if(mkfifo("atm2", 0666) == -1) {
printf("mkfifio failed2 : %d\n", errno) ;
if (errno==EEXIST ) {
printf("already exist. ignore\n") ;
}
else
return -1;
}
pthread_create(&t1, NULL, client, (void*)"atm1") ;
pthread_create(&t2, NULL, client, (void*)"atm2") ;
pthread_join(t1, (void**)&status) ;
pthread_join(t2, (void**)&status) ;
return 0;
}
you can access the atm1 and atm2 at the same time.
./customer atm1
./customer atm2
I have the following code which works fine with the sleep time. But I want to remove sleep and use semaphores to synchronize the threads ( so that the reader waits till data is available in shmptr.) How do I do that? What do I use:
sem_t mutex or
pthread_mutex_t mutex?
P.S : I tried using both above options separately (not shown in above code though) but didn't get desired result.Most of the times I was getting error in shmget in reader - "shmget in reader: No such file or directory".
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <string.h>
#include <semaphore.h>
#include <errno.h>
void *write(void *t)
{ char str[100] = "\0";
char str1[50];
key_t key;
char *shmptr;
int shmid;
if( (key = ftok("shmSemphore.c",'B')) == -1)
{
perror("ftok in writer");
pthread_exit(NULL);
}
if( (shmid = shmget(key, 1024, 0666 | IPC_CREAT)) == -1)
{
perror("shmget in writer");
pthread_exit(NULL);
}
shmptr = shmat(shmid,0,0);
if( (shmptr == (char *)(-1)))
{
perror("shmget in writer");
pthread_exit(NULL);
}
printf("ENter your text :- ");
//scanf("%s",str);
fgets(str,sizeof(str),stdin);
strcpy(shmptr,str);
sleep(2);
shmdt(shmptr);
if( shmctl(shmid, IPC_RMID, NULL) == -1 )
{
perror("shmctl");
exit(-1);
}
pthread_exit(NULL);
}
void *read(void *t)
{ sleep(2);
key_t key;
char *shmptr;
int shmid;
if( (key = ftok("shmSemphore.c",'B')) == -1)
{
perror("ftok in writer");
pthread_exit(NULL);
}
if( (shmid = shmget(key, 1024, 0)) == -1)
{
perror("shmget in reader");
pthread_exit(NULL);
}
shmptr = shmat(shmid,0,0);
if( (shmptr == (char *)-1))
{
perror("shmget in writer");
pthread_exit(NULL);
}
printf("your text:- %s \n",shmptr);
pthread_exit(NULL);
}
int main()
{
pthread_t reader,writer;
int rt;
long t = 0;
rt = pthread_create(&writer,NULL,write,(void *)t);
/*if(rt == -1)
{ perror("writer thread");
pthread_exit(NULL);
}*/
rt = pthread_create(&reader,NULL,read,(void *)t);
/*if(rt == -1)
{ perror("writer thread");
pthread_exit(NULL);
}*/
pthread_join(writer,NULL);
pthread_join(reader,NULL);
return 0;
}
I'm writing a coprocess program using pipe. It works fine when the child read some data, handle it and output it. But when I read all the data and handle it, it just pending. Any body have some idea? Thank you.
Here is the source code:
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
int main()
{
#define MAXSIZE 1024
char workload[MAXSIZE];
char result[MAXSIZE];
workload[strlen(workload)] = EOF;
int workload_size = strlen(workload);
int fd1[2], fd2[2];
int n;
pid_t pid;
if (pipe(fd1) < 0 || pipe(fd2) < 0) {
fprintf(stderr, "pipe error: %s\n", strerror(errno));
exit(1);
}
if ((pid = fork()) < 0) {
fprintf(stderr, "fork error: %s\n", strerror(errno));
exit(1);
} else if (pid > 0) {
close(fd1[0]);
close(fd2[1]);
while(fgets(workload, MAXSIZE, stdin) != NULL)
{
workload_size = strlen(workload);
if (write(fd1[1], workload, workload_size) != workload_size) {
fprintf(stderr, "write to pipe error: %s\n", strerror(errno));
exit(1);
}
if ((n = read(fd2[0], result, MAXSIZE)) < 0) {
fprintf(stderr, "read from pipe error: %s\n", strerror(errno));
exit(1);
}
if (n == 0) {
fprintf(stderr, "child closed the pipe\n");
exit(1);
}
result[n] = 0;
if (puts(result) == EOF) {
fprintf(stderr, "fputs error\n");
exit(1);
}
}
} else {
close(fd1[1]);
close(fd2[0]);
if (fd1[0] != STDIN_FILENO) {
if (dup2(fd1[0] ,STDIN_FILENO) != STDIN_FILENO) {
fprintf(stderr, "dup2 error to stdin.\n");
exit(1);
}
close(fd1[0]);
}
if (fd2[1] != STDOUT_FILENO) {
if (dup2(fd2[1] ,STDOUT_FILENO) != STDOUT_FILENO) {
fprintf(stderr, "dup2 error to stdout.\n");
exit(1);
}
close(fd2[1]);
}
if (execl("./a.out", "a.out", NULL) < 0) {
fprintf(stderr, "execl error: %s\n", strerror(errno));
exit(1);
}
exit(0);
}
return 0;
}
Here is the source code of a.out, it works well with this:
#include <stdio.h>
#include <string.h>
int main()
{
#define MAXSIZE 1024
char x[MAXSIZE];
int n;
while(scanf("%s", x) != EOF)
{
printf("len:%d %s", strlen(x), x);
fflush(stdout);
}
return 0;
}
But it seems just pending when I write the code like this:
#include <stdio.h>
#include <string.h>
int main()
{
#define MAXSIZE 1024
char x[MAXSIZE];
int n;
while(scanf("%s", x) != EOF);
printf("Ok\n");
fflush(stdout);
return 0;
}
The way you are calling scanf with %s may overflow the x buffer. You should at least modify the scanf with a width modifier.
#include <stdio.h>
#include <string.h>
int main()
{
#define MAXSIZE 1024
char x[MAXSIZE];
int n;
while(scanf("%1024s", x) != EOF)
{
printf("len:%d %s", strlen(x), x);
fflush(stdout);
}
return 0;
}
And similarly for your other program.
The reason your program is getting blocked is because your second a.out program is looped doing another scanf, when at the same time the parent program is trying to read a response back into result.
You should test and loop while not feof and you might use popen & pclose
You probably want to use some multiplexing system call like poll
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.