Error creating the Fifo - c

I have the following code and I'm using the function mkfifo to create a Fifo, but the problem is that when I run the program, I get the printf saying "Error creating the fifo". What could it be? Here's the code:
//gcc Exam.c -o p
//./p 5 /home/directoryFile.c
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/wait.h>
int main(int argc, char *argv[])
{
int studentId, children = 0, j, i, childNumber[15], fdFile, fdread, fdFifo, fdOpenFifo;
float bytesToRead;
char directory[50];
char *buffer = malloc(256), *textToSend = malloc(256), *buffer2 = malloc(256);
char childFifo[16];
system("clear");
if(argc-1 < 1)
{
printf("\nSome arguments are missing\n");
return EXIT_FAILURE;
}
studentId = atoi(argv[1]);
strcpy(directory,argv[2]);
if((studentId%2) == 0)
{
children = 15;
}
else
{
if((studentId%3) == 0)
{
children = 10;
}
else
{
if((studentId%5) == 0)
{
children = 7;
}
else
{
printf("\nThe studentId is invalid \n");
return EXIT_FAILURE;
}
}
}
struct stat fileInfo;
stat(argv[2],&fileInfo);
bytesToRead = fileInfo.st_size / children;
printf("Children: %d\n",children);
printf("File size: %lld\n",(long long int) fileInfo.st_size);
printf("Bytes: %.2f\n",bytesToRead);
fdFile = open(directory,O_RDONLY);
if(fdFile == -1)
{
printf("\nError opening file\n");
return EXIT_FAILURE;
}
for(i=0;i<children;i++)
{
sprintf(childFifo,"Child_%d",i);
fdFifo = mkfifo(childFifo,0777);
if(fdFifo == -1)
{
printf("\nError creating the fifo\n");
return EXIT_FAILURE;
}
childNumber[i] = fork();
if(childNumber[i] == -1)
{
printf("\nError creating the child\n");
return EXIT_FAILURE;
}
if(childNumber[i] == 0)
{
fdOpenFifo = open(childFifo,O_WRONLY);
if(fdOpenFifo == -1)
{
printf("\nError opening the fifo\n");
return EXIT_FAILURE;
}
fdread = read(fdFile,buffer,bytesToRead);
if(fdread == -1)
{
printf("\nError reading the file\n");
return EXIT_FAILURE;
}
//printf("%s",buffer);
//printf("\n\n------------------------\n\n");
write(fdOpenFifo,&buffer,sizeof(char));
//sprintf(textToSend,"%s%s",textToSend,buffer);
return EXIT_SUCCESS;
}
else
{
sleep(1);
read(fdOpenFifo,&buffer2,sizeof(char));
printf("\nI show the content of the file: %s\n",buffer2);
//waitpid(childNumber[i],NULL,WNOHANG);
close(fdOpenFifo);
}
}
//printf("\nI show the content of the file: %s\n",textToSend);
close(fdFile);
for(j=0;j<children;j++)
{
wait(NULL);
}
return EXIT_SUCCESS;
}
Here's the output for: ./p 5 /home/directoryFile.c
Children: 7
File size: 267
Bytes: 38.00
Error creating the fifo

Related

Merging two text files into new one (back and forth every new line) using C in Linux using system-calls

So the assignment is merging two text files in Linux using system calls:
1.txt:
Hello,
my class!
2.txt:
Today is
a very nice
day
NEW.txt:
Today is
Hello,
a very nice
my class!
day
Problem is I get (sorry for putting this as code sample):
Hello,
+Today is
my class!
a very nice
day
The + sign keeps changing between ("#", "(", "0", "_", ..) with each execution..
Why do I have extra new lines and this thing with the *?
Thank you in advance.
My code:
#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
int main(int argc, char* argv[])
{
int p1,p2,pNEW;
char Buff1[1], Buff2[1];
p1=open(argv[1],O_RDONLY);
p2=open(argv[2],O_RDONLY);
pNEW=open(argv[3],O_WRONLY|O_CREAT,0644);
while(read(p1,&Buff1,1)>0 || read(p2,&Buff2,1)>0)
{
do{
write(pNEW,&Buff1,1);
if((Buff1[0]=='\n'))
break;
}while(read(p1,&Buff1,1)>0);
do{
write(pNEW,&Buff2,1);
if((Buff2[0]=='\n'))
break;
}while(read(p2,&Buff2,1)>0);
}
close(p1);
close(p2);
close(pNEW);
}
you have to retain if you have read all your file or not, because the read in the first while will ... read, and that's not what you want.
Code edited after comment :
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <stdbool.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
bool WriteLineFromFile(int dst, int src, bool *srcTerminated)
{
int lastChar = EOF;
char currentChar;
ssize_t nbCharRead;
ssize_t nbCharWrite;
do {
if ((nbCharRead = read(src, &currentChar, 1)) < 0) {
fprintf(stderr, "%s : read(src, &buf, 1) : src=%d, errno='%s'.\n", __func__, src, strerror(errno));
return (false);
}
// End of file
if (nbCharRead == 0) {
(*srcTerminated) = true;
// Adding '\n' if necessary
if (lastChar != '\n' && lastChar != EOF) {
currentChar = '\n';
while ((nbCharWrite = write(dst, &currentChar, 1)) != 1) {
if (nbCharWrite < 0) {
fprintf(stderr, "%s : write(dst, &buf, 1) : dst=%d, errno='%s'.\n", __func__, dst, strerror(errno));
return (false);
}
sleep(1);
}
}
return (true);
}
// Writing a char into the dst file
while ((nbCharWrite = write(dst, &currentChar, 1)) != 1) {
if (nbCharWrite < 0) {
fprintf(stderr, "%s : write(dst, &buf, 1) : dst=%d, errno='%s'.\n", __func__, dst, strerror(errno));
return (false);
}
sleep(1);
}
lastChar = currentChar;
} while (currentChar != '\n');
return (true);
}
bool FileMerging(char *inputPathFile1, char *inputPathFile2, char *outputPathFile)
{
int inputFile1 = -1;
bool file1Terminated = false;
int inputFile2 = -1;
bool file2Terminated = false;
int outputFile = -1;
bool returnFunction = false;
// Openning all the file descriptor
if ((inputFile1 = open(inputPathFile1, O_RDONLY)) == -1) {
fprintf(stderr, "%s : open(inputPathFile1, O_RDONLY) : inputPathFile1='%s', errno='%s'.\n", __func__, inputPathFile1, strerror(errno));
goto END_FUNCTION;
}
if ((inputFile2 = open(inputPathFile2, O_RDONLY)) == -1) {
fprintf(stderr, "%s : open(inputPathFile2, O_RDONLY) : inputPathFile2='%s', errno='%s'.\n", __func__, inputPathFile2, strerror(errno));
goto END_FUNCTION;
}
if ((outputFile = open(outputPathFile, O_WRONLY | O_CREAT, 0644)) == -1) {
fprintf(stderr, "%s : open(outputPathFile, O_RDONLY) : outputPathFile='%s', errno='%s'.\n", __func__, outputPathFile, strerror(errno));
goto END_FUNCTION;
}
// Alternativly print a line from inputFile1 and inputFile2 to outputFile
do {
if (!file1Terminated) {
if (!WriteLineFromFile(outputFile, inputFile1, &file1Terminated)) {
goto END_FUNCTION;
}
}
if (!file2Terminated) {
if (!WriteLineFromFile(outputFile, inputFile2, &file2Terminated)) {
goto END_FUNCTION;
}
}
} while (!file1Terminated || !file2Terminated);
returnFunction = true;
/* GOTO */END_FUNCTION:
if (inputFile1 != -1) {
close(inputFile1);
}
if (inputFile2 != -1) {
close(inputFile2);
}
if (outputFile != -1) {
close(outputFile);
}
return (returnFunction);
}
int main(int argc, char *argv[])
{
if (argc != 4) {
fprintf(stderr, "This program wait 3 arguments on the command-line : inputFilePath1 inputPathFile2 outputPathFile.\n");
return (EXIT_FAILURE);
}
if (!FileMerging(argv[1], argv[2], argv[3])) {
return (EXIT_FAILURE);
}
return (EXIT_SUCCESS);
}

Close pipes get Bad file descriptor

Here is the code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
int main(int argc, char **argv) {
int num = 2;
pid_t pid;
int i;
int p1[num][2], p2[num][2];
for (i = 0; i < num; i++) {
if (pipe(p1[i]) == -1) {
perror("pipe");
exit(1);
}
if (pipe(p2[i]) == -1) {
perror("pipe");
exit(1);
}
}
for (i = 0; i < num; i++) {
if ((pid = fork()) == 0) {
if (close(p1[i][1]) != 0) {
perror("close");
exit(1);
}
if (close(p2[i][0]) != 0) {
perror("close");
exit(1);
}
printf("%d\n", getpid());
exit(0);
} else if (pid > 0) {
if (close(p1[i][0]) != 0) {
perror("close");
exit(1);
}
if (close(p2[i][1]) != 0) {
perror("close");
exit(1);
}
continue;
} else {
perror("fork");
exit(1);
}
}
for (i = 0; i < num; i++) {
if (close(p1[i][0]) != 0) {
perror("close1"); // <----error
}
if (close(p1[i][1]) != 0) {
perror("close");
}
if (close(p2[i][0]) != 0) {
perror("close");
}
if (close(p2[i][1]) != 0) {
perror("close2"); // <----error
}
}
for (i = 0; i < num; i++) {
if (wait(NULL) == -1) {
perror("wait");
exit(1);
}
}
return 0;
}
When I run this, it gives me this output
close1: Bad file descriptor
close2: Bad file descriptor
close1: Bad file descriptor
close2: Bad file descriptor
8798
8799
What I'm trying to do is to create two 2D array of pipes and fork num times.
The creating and running are working well, but some pipes can't close.
It seems that p1[i][0] and p2[i][1] are never closer properly.
The reason you're getting the EBADFD is you're attempting to close the same file descriptor twice.
I added a print statement in this first snippet of code to show/track what file descriptor is being closed. If you compile and run this you'll see that the error message appears right after you try to close the descriptor for a second time.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
int main(int argc, char **argv) {
int num = 1;
pid_t pid;
int i;
int p1[num][2], p2[num][2];
for (i = 0; i < num; i++) {
if (pipe(p1[i]) == -1) {
perror("pipe");
exit(1);
}
if (pipe(p2[i]) == -1) {
perror("pipe");
exit(1);
}
}
for (i = 0; i < num; i++) {
if ((pid = fork()) == 0) {
printf("Child closing: Pipe1 %d End %d\n", i, 1);
if (close(p1[i][1]) != 0) {
perror("close");
exit(1);
}
printf("Child closing: Pipe2 %d End %d\n", i, 1);
if (close(p2[i][0]) != 0) {
perror("close");
exit(1);
}
printf("%d\n", getpid());
exit(0);
} else if (pid > 0) {
printf("Parent closing: Pipe1 %d End %d\n", i, 0);
if (close(p1[i][0]) != 0) {
perror("close");
exit(1);
}
printf("Parent closing: Pipe1 %d End %d\n", i, 1);
if (close(p2[i][1]) != 0) {
perror("close");
exit(1);
}
continue;
} else {
perror("fork");
exit(1);
}
}
for (i = 0; i < num; i++) {
printf("Closing: Pipe1: %d End: %d\n", i, 0);
if (close(p1[i][0]) != 0) {
perror("close1"); // <----error
}
printf("Closing: Pipe1: %d End: %d\n", i, 1);
if (close(p1[i][1]) != 0) {
perror("close");
}
printf("Closing: Pipe2: %d End: %d\n", i, 0);
if (close(p2[i][0]) != 0) {
perror("close");
}
printf("Closing: Pipe2: %d End: %d\n", i, 1);
if (close(p2[i][1]) != 0) {
perror("close2"); // <----error
}
}
for (i = 0; i < num; i++) {
if (wait(NULL) == -1) {
perror("wait");
exit(1);
}
}
return 0;
}
Check for the pid and close the ends that weren't closed inside your first loop. This code assumes you are reading and writing on a specific pipe depending on the child's/parent's need. You may need to adjust to however your use case dictates:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
int main(int argc, char **argv) {
int num = 1;
pid_t pid;
int i;
int p1[num][2], p2[num][2];
for (i = 0; i < num; i++) {
if (pipe(p1[i]) == -1) {
perror("pipe");
exit(1);
}
if (pipe(p2[i]) == -1) {
perror("pipe");
exit(1);
}
}
for (i = 0; i < num; i++) {
if ((pid = fork()) == 0) {
if (close(p1[i][1]) != 0) {
perror("close");
exit(1);
}
if (close(p2[i][0]) != 0) {
perror("close");
exit(1);
}
printf("%d\n", getpid());
exit(0);
} else if (pid > 0) {
if (close(p1[i][0]) != 0) {
perror("close");
exit(1);
}
if (close(p2[i][1]) != 0) {
perror("close");
exit(1);
}
continue;
} else {
perror("fork");
exit(1);
}
}
for (i = 0; i < num; i++) {
if (pid == 0) {
if (close(p1[i][0]) != 0) {
perror("close1");
}
if (close(p2[i][1]) != 0) {
perror("close");
}
} else {
if (close(p1[i][1]) != 0) {
perror("close");
}
if (close(p2[i][0]) != 0) {
perror("close2");
}
}
}
for (i = 0; i < num; i++) {
if (wait(NULL) == -1) {
perror("wait");
exit(1);
}
}
return 0;
}

why this linux pseudo terminal program doesn't work?

Master reads input from stdin and writes to pty-master, slave reads input from pty-slave and writes to stdout.
But this code/program seems doesn't work.
The Master writes to pty-master is OK, but the slave hungs when reads from pty-slave.
anyone can help me? thx in advance.
#include <assert.h>
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
/*
* copy from apue
*/
int ptym_open(char *pts_name, int pts_namesz)
{
char *ptr;
int fdm, err;
if ((fdm = posix_openpt(O_RDWR)) < 0) {
assert(0);
return -1;
}
if (grantpt(fdm) < 0) {
assert(0);
return -1;
}
if (unlockpt(fdm) < 0) {
assert(0);
return -1;
}
if ((ptr = ptsname(fdm)) == NULL) {
assert(0);
return -1;
}
strncpy(pts_name, ptr, pts_namesz);
pts_name[pts_namesz - 1] = 0;
printf("pts_name:%s\n", pts_name);
return fdm;
}
int ptys_open(char *pts_name)
{
int fds;
if ((fds = open(pts_name, O_RDWR)) < 0) {
return -1;
}
return fds;
}
int pty_fork(int *ptrfdm, char *slave_name, int slave_namesz)
{
int fdm, fds;
pid_t pid;
char pts_name[1024];
if ((fdm = ptym_open(pts_name, sizeof(pts_name))) < 0) {
assert(0);
return -1;
}
if (slave_name != NULL) {
strncpy(slave_name, pts_name, slave_namesz);
slave_name[slave_namesz - 1] = 0;
}
if ((pid = fork()) < 0) {
assert(0);
return -1;
} else if (pid == 0) {
if (setsid() < 0) {
assert(0);
}
if ((fds = ptys_open(pts_name)) < 0) {
assert(0);
}
close(fdm);
if (dup2(fds, STDIN_FILENO) != STDIN_FILENO) {
assert(0);
}
//if (dup2(fds, STDOUT_FILENO) != STDOUT_FILENO) {
// assert(0);
//}
//if (dup2(fds, STDERR_FILENO) != STDERR_FILENO) {
// assert(0);
//}
if ((fds != STDIN_FILENO) && (fds != STDOUT_FILENO) && (fds != STDERR_FILENO)) {
close(fds);
}
return 0;
} else {
*ptrfdm = fdm;
return pid;
}
}
int loop(int ptym)
{
pid_t pid;
int nread;
#define BUFFSIZE 512
char buf[BUFFSIZE];
if ((pid = fork()) < 0) {
assert(0);
} else if (pid == 0) {
while (1) {
if ((nread = read(STDIN_FILENO, buf, BUFFSIZE)) < 0) {
int errr = errno;
printf("%s\n", strerror(errr));
assert(0);
} else if (nread = 0) {
break;
}
if (write(ptym, buf, nread) != nread) {
int errr = errno;
printf("%s\n", strerror(errr));
assert(0);
}
fsync(ptym);
}
exit(0); // child
}
while (1) {
if ((nread = read(ptym, buf, BUFFSIZE)) <= 0) {
printf("%d break read\n", getpid());
break;
}
if (write(STDOUT_FILENO, buf, nread) != nread) {
assert(0);
}
}
}
int main(void)
{
int fdm;
char slave_name[1024];
pid_t pid = pty_fork(&fdm, slave_name, sizeof(slave_name));
if (pid < 0) {
assert(0);
} else if (pid == 0) {
int nread;
char buf[1024];
while(1){
if ((nread = read(STDIN_FILENO, buf, 3)) < 0) {
break;
}
printf("buf:%s\n", buf);
}
} else {
printf("child:%d#%s\n", pid, slave_name);
loop(fdm);
}
return 0;
}
Besides opening the pseudo-terminal, you have to initialize it (something referred to as line discipline). I don't see any of that in your example. You could compare with luit, which does do this (look for instance at the openTty function in sys.c).
Further reading:
Notes 7: Terminal I/O
Writing a Kernel Line Discipline
What are the responsibilities of each Pseudo-Terminal (PTY) component (software, master side, slave side)?

Semget and fork(): no such file or directory

I have a problem with accessing semaphore in child process. I can't get already created semaphore through semget() when in child process.
Here is make_semaphores() function;
int make_semaphores(key_t key)
{
int semid;
semid = semget(key, 3, IPC_CREAT|/*IPC_EXCL|*/0666);
if(semid == -1)
{
perror("Creating an array of semaphores");
exit(1);
}
if(semctl(semid,0,SETVAL, (int)MAX)==-1)
{
perror("Initializing 'empty' semaphore");
exit(1);
}
if(semctl(semid,1,SETVAL, (int)0)==-1)
{
perror("Initializing 'full' semaphore");
exit(1);
}
if(semctl(semid,2,SETVAL, (int)1)==-1)
{
perror("Initializing mutex");
exit(1);
}
return semid;
}
Here is allocate_memory function:
int* allocate_memory(int *buf, key_t key)
{
int shmid;
shmid=shmget(key,(MAX+1)*sizeof(int),IPC_CREAT|/*IPC_EXCL|*/0666);
if(shmid==-1)
{
perror("Creating shared memory segment");
exit(1);
}
buf=(int*)shmat(shmid,NULL,0);
if(buf==NULL)
{
perror("Including shared memory segment");
exit(1);
}
buf[0]=0;
return buf;
}
I searched and didn't find a satisfying answer, so sorry if I'm needlessly spamming. Thanks in advance for any kind of help.
Here is full main:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/shm.h>
#include <sys/wait.h>
#define S1KEY 467221
#define S2KEY 379231
#define S3KEY 217411
#define MAX 10
int main(int argc, char* argv[])
{
srand(time(NULL));
int fork_id;
int *buf1, *buf2, *buf3;
int semid1, semid2, semid3, i, j, r, smd1;
int prod_info[3]={0,0,0}, cons_info[3]={0,0,0};
printf("-------------------\n");
buf1=allocate_memory(buf1,S1KEY);
buf2=allocate_memory(buf2,S2KEY);
buf3=allocate_memory(buf3,S3KEY);
printf("-------------------\n");
semid1=make_semaphores(S1KEY);
semid2=make_semaphores(S2KEY);
semid3=make_semaphores(S3KEY);
smd1=semget(S1KEY,3,0666);//here is okay
if(semid1!=smd1)
{
printf("semid1: %d smd1: %d\n",semid1,smd1);
perror("S1KEY does not exist");
}
printf("----------------\n");
for(i=0;i<4;++i)
{
fork_id=fork();
if(fork_id<0)
{
perror("fork()");
exit(1);
}
else if(fork_id==0)// one of four children
{
smd1=semget(S1KEY,3,0666);//here is NOT okay, smd returns -1
if(smd1==-1)
{
printf("[%d]semid1: %d smd1: %d\n",i,semid1,smd1);
perror("S1KEY does not exist");
}
if(i==3) //producer for 3 buffers
{
for(j=0;j<100;j++)
{
r=(rand()%3)+1;
printf("-------------------------\n");
if(r==1)
{
printf("Prod:1||%d||%d\n",prod_info[0],getval_semaphores(semid1,0));
printf(" %d\n", buf1[0]);
produce(buf1,semid1,i);
prod_info[0]++;
}
else if(r==2)
{
printf("Prod:2||%d||%d\n",prod_info[1],getval_semaphores(semid2,0));
printf(" %d\n", buf2[0]);
produce(buf2,semid2,i);
prod_info[1]++;
}
else
{
printf("Prod:3||%d||%d\n",prod_info[2],getval_semaphores(semid3,0));
printf(" %d\n", buf3[0]);
produce(buf3,semid3,i);
prod_info[2]++;
}
}
printf("Produced: 1:%d 2:%d 3:%d\n", prod_info[0],prod_info[1],prod_info[2]);
exit(1);
}
else if(i==2) //3 consumers for 3 buffers (1,2,3)
{
for(j=0;j<100;j++)
{
//usleep(1);
printf("Cons:3\n");
printf("%d\n", buf1[0]);
printf("%d\n", buf2[0]);
printf("%d\n", buf3[0]);
cons_info[0]=consume(buf1,semid1);
cons_info[1]=consume(buf2,semid2);
cons_info[2]=consume(buf3,semid3);
printf("Buf1: %d\n",cons_info[0]);
printf("Buf2: %d\n",cons_info[1]);
printf("Buf3: %d\n",cons_info[2]);
}
exit(1);
}
else if(i==1) //2 consumers for 2 buffers (2,3)
{
for(j=0;j<100;j++)
{
//usleep(1);
printf("Cons:2\n");
printf("%d\n", buf2[0]);
printf("%d\n", buf3[0]);
cons_info[1]=consume(buf2,semid2);
cons_info[2]=consume(buf3,semid3);
printf("Buf2: %d\n",cons_info[1]);
printf("Buf3: %d\n",cons_info[2]);
}
exit(1);
}
else if(i==0) //1 consumer for 1 buffer (3)
{
for(j=0;j<100;j++)
{
//usleep(1);
printf("Cons:1\n");
printf("%d\n", buf3[0]);
cons_info[2]=consume(buf3,semid3);
printf("Buf3: %d\n",cons_info[2]);
}
exit(1);
}
}
else
{
if(i==4) //Parent, waits after making children
{
smd1=semget(S1KEY,3,0666);
if(semid1!=smd1)
{
printf("[%d]semid1: %d smd1: %d\n",i,semid1,smd1);
perror("S1KEY does not exist");
}
printf("Parent\n");
wait(NULL);
}
}
}
break_semaphores(semid1);
break_semaphores(semid2);
break_semaphores(semid3);
return 0;
}
I am afraid you misunderstand how the creation of processes works.
Parent/Child process model
The process creation model is generally as follows
pid_t p1 = fork();
if (p1 < 0) {
perror("problem forking");
}
else if (p1 == 0) {
// parent process
printf("parent process\n");
// do_parent_process_stuff()
}
else {
// child process
printf("child process: %d\n", p1);
// do_child_process_stuff()
}
Correction to your code
As for your code, in your main function you will want to replace
for(i=0;i<4;++i)//parent creates four children
{
fork_id=fork();
smd1=semget(S1KEY,3,0666);//here is NOT okay (when in child process), smd returns -1
if(smd1==-1)
{
printf("[%d]semid1: %d smd1: %d\n",i,semid1,smd1);
perror("S1KEY does not exist");
}
//the rest
with something like this
/* Start children. */
for (i = 0; i < n; ++i) {
if ((pids[i] = fork()) < 0) {
perror("error forking");
}
// parent
else if (pids[i] == 0) {
exit(0);
}
// child process
else {
smd1=semget(S1KEY,3,0666);
if(smd1==-1)
{
printf("[%d]semid1: %d smd1: %d\n",i,semid1,smd1);
perror("S1KEY does not exist");
}
else {
// do stuff with semaphores
}
}
}

How to check dynamic allocation

How can i check if i am using right the dynamic allocation in my C code.
It is an assignment for uni. When i put my code in the auto corrector system of my professor i get an error for dynamic allocation. My code :
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <stdarg.h>
#include <sys/types.h>
#include <unistd.h>
#include <ctype.h>
int main( )
{
for(;;)
{
char *cmd,*splitcmd;
//*pr0,*pr1,*pr2 ;
size_t bufsize = 1024;
char pr1[bufsize];
char pr2[bufsize];
char pr0[bufsize];
//char pr3[40];
int i,j,nargc=0,characters;
char **cmdArray;
//size_t bufsize = 1024;
// size_t bufsizecmd =1024;
pid_t pid,wpid;
int status = 0;
char pr='$';
char exit1[10]="exit";
char *path;
path = getenv("PATH");
putchar(pr);
cmd = malloc(bufsize * sizeof*cmd);
characters = getline(&cmd,&bufsize,stdin);
//printf("cmd===> %s characters===> %d \n",cmd,characters);
if(cmd[characters-1]=='\n' )
{
cmd[characters-1]='\0';
characters--;
}
//printf("cmd===> %s characters===> %d \n",cmd,characters);
cmdArray = malloc(bufsize*sizeof*cmdArray );
for ( i = 0; i < bufsize; i++ )
{
cmdArray[i]=malloc(bufsize*sizeof*cmdArray );
}
splitcmd=strtok(cmd," ");
// printf(" cmd==== %s\n",cmd);
while((splitcmd))
{
strcpy(cmdArray[nargc],splitcmd);
if(cmdArray[nargc][(strlen(cmdArray[nargc]))-1]==' ')
cmdArray[nargc][(strlen(cmdArray[nargc]))-1]='\0';
//printf(" nargc====%d cmdArray===[%s] \n",nargc,cmdArray[nargc]);
nargc++;
splitcmd = strtok(NULL," ");
}
//printf(" pr0 %s \n",pr0);
//printf(" pr1 %s \n",pr1);
//printf(" pr2 %s \n",pr2);
strcpy(pr0,cmdArray[0]);
if (strcmp( pr0, exit1) == 0 )
{
//printf("---------->Eksodos apo to programma<---------- \n");
free(cmd);
return (0);
exit(0);
//return (0);
}
else
{
if ((pid=fork()) == 0)
{
if(nargc ==1 )
{
strcpy(pr0,cmdArray[0]);
char *argv[] = {path,NULL};
execvp(pr0,argv);
for ( i = 0; i < bufsize; i++)
{
free(cmdArray[i]);
}
free(cmdArray);
free(cmd);
exit(0);
}
else if(nargc==2)
{
strcpy(pr0,cmdArray[0]);
strcpy(pr1,cmdArray[1]);
char *argv[] = {pr0,pr1,NULL};
execvp(pr0,argv);
exit(0);
for ( i = 0; i < bufsize; i++)
{
free(cmdArray[i]);
}
free(cmdArray);
free(cmd);
exit(0);
}
else
{
strcpy(pr0,cmdArray[0]);
strcpy(pr1,cmdArray[1]);
strcpy(pr2,cmdArray[2]);
//printf("cmdddddddd****====%s \n",*cmdArray);
char *argv[] = {pr0,pr1,pr2,NULL};
execvp(argv[0],argv);
for ( i = 0; i < bufsize; i++)
{
free(cmdArray[i]);
}
free(cmdArray);
free(cmd);
exit(0);
}
}
wait(&status);
}
}
}
There is no corresponding free() for cmd = (char *)malloc(bufsize * sizeof(char));.
Also, this code block:
strcpy(pr0,cmdArray[0]);
if (strcmp( pr0, exit1) == 0 )
{
//printf("---------->Eksodos apo to programma<---------- \n");
free(cmd);
return (0);
exit(0);
//return (0);
}
will need this
for ( i = 0; i < bufsize; i++)
{
free(cmdArray[i]);
}
free(cmdArray);
before the
free(cmd);
In this code block, there are two exit() calls:
else if(nargc==2)
{
strcpy(pr0,cmdArray[0]);
strcpy(pr1,cmdArray[1]);
char *argv[] = {pr0,pr1,NULL};
execvp(pr0,argv);
exit(0);
for ( i = 0; i < bufsize; i++)
{
free(cmdArray[i]);
}
free(cmdArray);
free(cmd);
exit(0);
}
The one immediately after the execvp(pr0,argv); prevents any of the free() calls from being executed. It needs to be removed.

Resources