Question
I have the following code....from the previous lab....the above needs to be added to what is already existing. ALSO MODIFY SEMAPHORES TO USE pthreads instead
I have the following code....from the previous lab....the above needs to be added to what is already existing. ALSO MODIFY SEMAPHORES TO USE pthreads instead of the pipe constructs P() & V()
#include
#include
#include
#include
#include
#include
#include
void printStat(char *filename);
//Main
int main(int argc, char *argv[])
{
//Process Id (storing)
pid_t pid;
int j;
//printf("Welcome to Project Three );
// For loop*/
for (j = 1; j
{
//forking process
pid=fork();
//Child process (if condition)
if (pid == 0)
{
printStat(argv[j]);
return 0;
}
else{
}
}
//Waiting for the child to finish
while( wait(NULL) > 0 );
//Printing the current running process
system("ps -H");
printf("The End! ");
exit(0);
}
////////////Biginning of lab 3////////////////////////
void printStat(char *filename)
{
//struct
struct stat sb;
printf("File: %s ", filename);
//if condition
if (stat(filename, &sb) == -1)
{
printf("error in stat ");
exit(1);
}
//if condition to see if the current user is the file's owner
if((sb.st_uid == getuid())&&(sb.st_gid == getgid()))
{
printf("You Have Owner Permissions: ");
if(sb.st_mode & S_IRUSR)
printf("Read ");
if(sb.st_mode & S_IWUSR)
printf("Write ");
if(sb.st_mode & S_IXUSR)
printf("Execute ");
}
//check if the file's Group is same current user's Group
else if ((sb.st_uid != getuid())&&(sb.st_gid == getgid()))
{
printf("You Have Group Permissions: ");
if(sb.st_mode & S_IRGRP)
printf("Read ");
if(sb.st_mode & S_IWGRP)
printf("Write ");
if(sb.st_mode & S_IXGRP)
printf("Execute ");
}
else
{
printf("You Have Other Permissions: ");
if(sb.st_mode & S_IROTH)
printf("Read ");
if(sb.st_mode & S_IWOTH)
printf("Write ");
if(sb.st_mode & S_IXOTH)
printf("Execute ");
}
printf(" ");
}//End Lab 3
The project developed in Lab 2 and #3 part 1 will be extended by this one. Using stat you wl need to open each file within a child process and determine the following: the user ID and the group ID and then compare these to the current user. The 3 cases are: if the user is the owner, the user is in the same group as the owner, or if the owner is outside of the user group. After that information is printed, then prinhe combination of file permissions that apply to this file: ead, wrte, execute. Sample code: man stat, man 2 stat, man getpwuid, man readdir, and fileaccess.c Program requirements: 1. The program should take either filenames or wildeards (c for example) as input 2. Create as many child processes as there are files on the command line 3. (you may remove the ps-H) The parent process should wait for all the children to finish and then print "ps -H" . For each child process . Print if you have owner, group, or general permissions. Hint: getuid0: & getgid0 .For the appropriate type, print what type of permissions you have: read, write, execute 5. If no filename is specified on the command prompt, use dirent to retrieve all files in the current folder. Modular code wil make this task much easier, but is not required 6. Use the output from stat to determine the owner of the file, and then use the passwd structure on the owner of the file to determine the home directory of the user that owns that file. 7. Your print output may occasionally be garbled. How would you fix this? (Hint: Semaphores implemented as pipes see below) Separate program operations and print statements as best as possible. Consider the printf statements as a critical section. Do not use delays or wait inside your main loop, or make child processes wait in any way 10% Extra Credit: modify the semaphores to use pthreads instead of the pipe constructs P() & V() that are provided below
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started