I am writing a simple shell in C. Right now when I run the command cd only work in the current environment that I am. So for example if I am in the Desktop folder I can only cd for a folder inside Desktop, I can't go to other folders in my computer.
How do I control this change of directories?
Thant's my cd function right now. Please let me know if you need more information.
int cmd_cd(char **args)
{
if (args[1] == NULL) {
fprintf(stderr, "expected argument to \"cd\"\n");
} else {
if (chdir(args[1]) != 0) {
fprintf(stderr, args[1],strerror(errno));;
}
}
return 1;
}
Here is my exec function that calls cd
char* builtCommandList[] = {"cd", "exit"};
int builtinSize = sizeof(builtCommandList) / sizeof(char *);
int (*builtin_func[]) (char **) = {
&cmd_cd,
&cmd_exit
};
int execute(char **args){
if(args[0] == NULL)
return 1;
for(int i =0; i<builtinSize;i++){
if (strcmp(args[0], builtCommandList[i]) == 0) {
printf("function is here");
return (*builtin_func[i])(args);
}
}
pid_t pid, wpid;
int status;
pid = fork();
if (pid == 0) {
// Child process
if (execvp(args[0], args) == -1) {
fprintf(stderr, "error in child process");
}
exit(EXIT_FAILURE);
} else if (pid < 0) {
// Error forking
fprintf(stderr, "error forking");
} else {
// Parent process
do {
wpid = waitpid(pid, &status, WUNTRACED);
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
}
return 1;
}
I created following method in C to create a shared memory segment to store counter value. But I can't store data in this segment.When I try to print the value of the counter it gives me a garbage value.Whats wrong with this code?
CreateCounter()
{
key = ftok(".",'B');
shmCntid = shmget(key,COUNTER_SIZE,IPC_CREAT|0666);
if(shmCntid == -1 )
{
perror("shmget");
exit(1);
}
else
{
printf("Creating new Sahred memory sement\n");
cntPtr = shmat(shmCntid,0,0);
if(cntPtr == -1 )
{
perror("shmat");
exit(1);
}
}
}
This method is called inside the main method as follows.
int *cntPtr;
int rowCnt;
sem_t s;
sem_t c;
sem_t r;
int main(int argc, int *argv[])
{
int pid, pid2, pid3, i;
CreateBuf1();
CreateBuf2();
CreateCounter();
GetInput(argv[1],*buf1Ptr);
sem_init(&c, 0, 1);
sem_init(&r, 0, 1);
sem_init(&s, 0, 1);
for( i = 0 ; i < 9; i++)
{
pid = fork();
if(pid < 0)
{
printf("Fork error !\n");
}
else if (pid == 0)
break;
}
if(pid < 0)
{
printf("Fork error !\n");
}
else if (pid == 0)
{
sem_wait(&r);
Grp1 (i,i);
cntPtr+=rowCnt;
sleep(1);
sem_post(&r);
sem_post(&c);
exit(0);
}
else
{
wait(NULL);
}
pid2 = fork();
if(pid2 < 0)
{
printf("Fork error !\n");
}
else if (pid2 == 0)
{
sem_wait(&c);
Grp2(9);
cntPtr+=colCnt;
sleep(1);
sem_post(&c);
exit(0);
}
else
{
wait(NULL);
}
// This space is to print the values..............
shmctl(shmBuf1id,IPC_RMID,0);
shmctl(shmBuf2id,IPC_RMID,0);
shmctl(shmCntid,IPC_RMID,0);
return 0;
}
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;
}
I want to handle multiple pipes in c via a recursive function. I could not figure it what is wrong. Whether I did not hook up the pipes properly or I left some process hanging. Please give some advice.
Assume: token[0] = "ls"; token[1] = "|"; token[2] = "sort"; token[3] = "|"; token[4] = "more";
int pipeExecution(char *token[]) {
int isPipe = 0;
int fds[2] = {0};
pid_t pid;
for (int i = 0; token[i] != NULL; i++) {
if (strcmp(token[i], "|") == 0) {
isPipe = 1;
token[i] = NULL;
if (pipe(fds) < 0) {
perror("Can not pipe\n");
exit(EXIT_FAILURE);
}
pid = fork();
if (pid < 0) {
perror("Can not fork() **1\n");
exit(EXIT_FAILURE);
} else {
if (pid > 0) {
pid = fork();
if (pid < 0) {
perror("Can not fork() **2\n");
exit(EXIT_FAILURE);
} else {
if (pid > 0) { // parent
wait(NULL);
} else { // child 2 - excuse the command after a "|"
close(fds[1]); // does not write to pipe
if (dup2(fds[0], STDIN_FILENO) < 0) {
perror("Can not dup2()\n");
exit(EXIT_FAILURE);
}
printf("execute tokens after |\n");
pipeExecution(token + (i + 1));
}
}
} else { // child 1 - excuse the command before a "|"
printf("execute tokens before |\n");
close(fds[0]); // does not read from pipe
if (dup2(fds[1], STDOUT_FILENO) < 0) {
perror("Can not dup2()\n");
exit(EXIT_FAILURE);
}
execvp(token[0], token);
}
}
break;
}
}
if (isPipe == 0) {
perror(token[0]);
pid = fork();
if (pid < 0) {
perror("Can not fork()\n");
exit(EXIT_FAILURE);
}
if (pid > 0) {
wait(NULL);
} else {
execvp(token[0], token);
}
}
return 0;
}
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
}
}
}