5. Answer the questions on the following program:
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
void ouch(int sig)
{
printf("OUCH! -‐ I got signal %d\n", sig);
}
int main()
{
signal(SIGINT, ouch);
signal(SIGQUIT, ouch);
while(1) {
printf("Hello World!\n");
sleep(1);
}
}
1) What is the output of just running the program?
2) What will happen if you try to use both keyboard signals of
terminal interrupt and
terminal quit: Ctrl-‐C and Ctrl-‐\ to kill the process?
3) Give exactly necessary commends which can just terminate this
process from shell.
Dear Student,
Here i have explained the C program as per the requirement.
NOTE:1 Please note that the below programs have been tested on ubuntu 14.04 system and compiled under gcc compiler.
Note: This program will also works in other IDEs.
--------------------------------------------------------------------------------------------------------------------------------------
Program..1: -------------------------------------------------------------------------------------------------------------------------------------
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
void ouch(int sig)
{
printf("OUCH! -‐ I got signal
%d\n", sig);
}
int main()
{
signal(SIGINT, ouch);
signal(SIGQUIT, ouch);
while(1) {
printf("Hello World!\n");
sleep(1);
}
}
----------------------------------------------------------------------------------------------------------------------------------------
Answer 1:
1) What is the output of just running the program?
Explanation: If you run the above program then after every 1 second Hellow, World will be printed on the screen.
Here i have attached the expected output of the program...
Output:
-------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------
Answer:2
2) What will happen if you try to use both keyboard signals of terminal interrupt and terminal quit: Ctrl-‐C and Ctrl-‐\ to kill the process?
Explanation: If you try to press ctrl+c and ctrl+\ then program is not going to terminate. For each signal it will print a different message below screen shot shows this..
if press ctrl+c it will display : ^COUCH! -‐ I got signal 2
if press ctrl+\ it will display : ^\OUCH! -‐ I got signal 3
here is the output:
Output:
-------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------------
Answer No:3
3) Give exactly necessary commends which can just terminate this process from shell.
To stop the program press ctrl+z.
hence pressing ctrl+z just terminate the program or stopped the program.
below screen shot shows this..
-----------------------------------------------------------------------------------------------------------------------------------------
Kindly Check and Verify Thanks...!!!
5. Answer the questions on the following program: #include <signal.h> #include <stdio.h> #include <unistd.h> void ouch(int...
operating system programming
i need ans and explen after 20 min
2 When we are implementing the following program, then we press "CTRL+C" on the keyboard; that will cause: #include <stdio.h> #include<signal.h> #include <stdlib.h> #include<unistd.h> void handle_sigint (int sig) { } printf("Caught signal $d\n", sig); int main(int argc, char *argv[]) { signal (SIGCONT, handle_sigint); while (1) { printf("the process id is $d \n",getpid()); sleep (1); } return 0; } O print the sentence" Caught signal 2" on the terminal 53,65,67,37,14,98,122,124,183...
I have the following code in C: void sighandler(int sig) { printf("exiting child..."); } int main(int argc,char* argv[]) { if(argc > 1) { char* commandName; for(int i=2;i { forkChild = fork(); signal(SIGINT,sighandler); if(forkChild == 0) { execlp(commandName,commandName,argv[i],NULL); exit(0); } else { wait(NULL); } } My problem is that I would like to kill the child with ^C but leave the parent running....
What is the output? #include <sys/types.h> #include <sys/wait.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> int main() { int x = 5; int y = 2; int z = 30; x = fork(); y = fork(); if (x != 0) printf("Type 1\n"); if (y != 0) printf("Type 2\n"); z = fork(); if (x > 0 || y > 0 || z > 0) printf("Type 3\n"); if (x == 0 && y == 0 && z != 0) printf("Type 4\n"); if (x...
Complete this program in C #include<stdio.h> int main(void) { int sumA(__, __); int answer; //the two parameters are start and end addresses of intA[] answer = sumA(......); printf("..........", intA); printf("The sum of integers in intA is %d.\n", answer); return 0; } //the two parameters of sumA(__, __) are start and end addresses of intA[] int sumA(..........) { ……………………. return ............. }
What is going to be the output of the following program: # include <stdio.h> int main(void) {struct dob {int month; int day; int year;}; struct student {int sid; int yearOfAdmission; float currentGPA; struct dob bday; struct student stdnt = {123, 2015, 8.3, 11, 26, 1993}; printf(" Id is: %d \n", stdnt.sid); printf("Year of admission is: %d \n", stdnt.year OfAdmission); printf("Current GPA is: %.2f\n\n", stdnt.currentGPA); printf("Date of Birth is: %d/%d/%d", stdnt. bday. month, stdnt. bday.day, stdnt. bday.} return 0;}
What is the functionality of the following code? #include #include mainO <stdio.h> <unistd.h> int i,j; j-0: printf ("Ready to fork.n) i-fork; if 0) this code.\n"); printf "The child executes for (i-0; i?5; i++) printf("Child j-dn".j); else j-vaito printf("The parent executes this code. Ln" printf ("Parent j-dn",j);
Problem la Points (20) Draw the process tree for the program shown below. #include<sys/types.h> #include<stdio.h> #include<unistd.h> int value = 5; int main() pid_t pid; pid = forko; if(pid ==0) { */ Child Process */ forkO; value += 15; return 0; else if (pid > 0) {/* Parent process */ forkO; forkO; wait(NULL); printf("Parent: value = %d", value); /* LINE A */ return 0;
Question 8 Predict the output of the following C program: 5 #include <stdio.h> void binaryCode(int n) printf("1"); if(n=2) return; binaryCode(n-1); int i = 0; for(; i<n;i++) printf("O"); binaryCode(n-2); } void main() { binaryCode(3); 12pt v Paragraph Β Ι Ο Avor T²v ...
Write as a shell script
Write as a shell script # include <stdio.h> int main (void) {int area_code, prefix, number; printf("Enter phone number [(999) 999-9999]:"); scanf("(%d)%d-%d", &area_code, & prefix, & number); printf("You entered %d-%d-%d \n", area_code, prefix, number); return 0;}
The operating system is Ubuntu 18.04
hello.c
#include <stdio.h>
int main(int argc, char *argv[]) {
printf("Hello world!\n");
return 0;
}
syscall-hello.c
#include <unistd.h>
#include <sys/syscall.h>
char *buf = "Hello world!\n";
int main(int argc, char *argv) {
size_t result;
/* "man 2 write" to see arguments to write syscall */
result = syscall(SYS_write, 1, buf, 13);
return (int) result;
}Download and compile hello.ce and syscall-hello.com. Compile them statically and dynamically. How do the library and system calls produced by them compare...