1

I have to write a program which generates child process than ends parent process and after that that created child process has to ask user to input new working directory, change it and print path to its new working directory. I wrote this, but scanf is not working ("It's not asking user to input something, program just ends) and the path is not changing... I've tried to set new directory to char *newdirectory="home/usr/desktop" and that didn't change working directory too

#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/stat.h>

int main()
{
    int pid;
    char directory[1024];
    char newdirectory[1024];        
    pid=fork(); 
    if(pid<0)
    {
        printf("\n Error ");
        exit(1);
    }
    else if(pid==0)
    {
        printf("I'm child \n ");
        printf(" My PID: %d \n",getpid());
        getcwd(directory, sizeof(directory));
        printf(" My current working directory is: %s\n", directory);
        printf(" Enter the new path\n");
        scanf("%s", &newdirectory);
        chdir(newdirectory);
        getcwd(directory, sizeof(directory));
        printf(" Path changed to: %s\n", directory);
        exit(0);
    }
    else
    {
        printf("I'm a parent \n ");
        printf("My PID is %d \n ",getpid());
        printf("Bye bye \n");
        exit(1);
    }
    return 0;
}

Thank you for your time, effort and all the help to understand :)

Sz3jdii
  • 507
  • 8
  • 23

1 Answers1

3

You have two mistakes,

  • Usage of scanf()
  • Not waiting for child to finish its task as parent.

The following works.

#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/stat.h>

int main()
{
    int pid;
    char directory[1024];
    char newdirectory[1024];
    pid=fork();
    if(pid<0)
    {
        printf("\n Error ");
        exit(1);
    }
    else if(pid==0)
    {
        printf("I'm child \n ");
        printf(" My PID: %d \n",getpid());
        getcwd(directory, sizeof(directory));
        printf(" My current working directory is: %s\n", directory);
        printf(" Enter the new path\n");
        scanf("%1023s", newdirectory);
        chdir(newdirectory);
        getcwd(directory, sizeof(directory));
        printf(" Path changed to: %s\n", directory);
        exit(0);
    }
    else
    {
        wait(0);
        printf("I'm a parent \n ");
        printf("My PID is %d \n ",getpid());
        printf("Bye bye \n");
        exit(1);
    }
}
  • 1
    First of all thank you so much! It's working and i understand everything but there's one question for you In task description it is written that "Parent process has to generate child process and before child do anything, parent has to end its "life"" and after parent's death, child has to ask for new path etc. I think it's mistake in task description because in my opinion we will create an orphaned process but i'm not 100% sure... What do you think? – Sz3jdii May 22 '18 at 12:48
  • 1
    @szeejdi Yes, I agree with you. The case brings about orphaned process. _Before child do anything, parent has to end its "life""_ is wrong. – Soner from The Ottoman Empire May 22 '18 at 12:49
  • Ok :D Thank you, I'll accept you answer as solution :) – Sz3jdii May 22 '18 at 12:50