To get a a better understanding of fork() system call , i am randomly playng between fork() and print statement but got stuck in one such code ,the code is
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
int main()
{
printf("\n my process id %d \n",getpid());
pid_t pid1=fork();
printf("\nPID:=%d fork returned %d\n",getpid(),pid1);
pid_t pid2=fork();
printf("\nPID:=%d fork returned %d\n",getpid(),pid2);
pid_t pid3=fork();
printf("\nPID:=%d fork returned %d\n",getpid(),pid3);
return 0;
}
Here is the output obtained-:
I am not able to get the print sequence it is following.Only thing i am getting is that first the process with PID 5079 is executing ,and for all the fork() system call,it is returning child PID to the parent process.I am not getting the further sequence.
The fork() might seem a little confusing at first, but its actually pretty simple. What it does is copy the current process into a new process in another memory location (copy everything, its data, code, current instruction, ...).
So we started with a process with pid = 5079, when we got to the fork call, a child process with pid = 5080 was created, and it has the same code of the parent process.
// Parent Process // // Child Process //
#include<stdio.h> #include<stdio.h>
#include<unistd.h> #include<unistd.h>
#include<stdlib.h> #include<stdlib.h>
int main() int main()
{ {
printf("\... printf("\...
pid_t pid1=fork(); pid_t pid1=fork();
printf("\nPI... //Next line// printf("\nPI... //Next line//
pid_t pid2=fork(); pid_t pid2=fork();
printf("\nPID:=... printf("\nPID:=...
pid_t pid3=fork(); pid_t pid3=fork();
printf("\nPID:=... printf("\nPID:=...
return 0; return 0;
} }
before we continue following the code, the return value of the fork call is as follows: inside the process that called the fork(), the return value is the pid of the child process (pid1 variable in Parent Process = 5080), and inside the child process, the output is 0 (pid1 variable in Child Process = 0).
So the print statement after the fork will be executed by the Parent Process and the Child Process with different getpid() value and different pid1 values, the parent has getpid() = 5079, and pid1 = child's pid = 5080 (you can see this in the third line of the output). The child will make its own print statement, with getpid() = 5080 and pid1 = 0, you can see this in the 8th line of the output, but why the 8th line!!!
The operating system schedules the process, that is, it decides which process the CPU will work on and for how long. so it seems that the OS decided that the parent process (pid = 5079) should run for a little longer, and left the child process (pid = 5080) waiting for the CPU to execute its instructions.
So process 5079 went on with the next fork, creating a new child process with pid = 5081. Then it printed what we expect in the third line, then it went on to the last fork creating process 5082, printing what we would expect in the forth line, then terminating (process 5079 terminated, leaving 5080,5081,5082 waiting, and Adopted by the OS, they need to have a parent, but this isn't important to the output).
Now that 5079 terminated, we have 3 processes waiting in the memory for the CPU to work them. The operating system must decide which process to run, and it seems to have choose the process that is closest to terminate, which is process 5082, let's look at the remaining instructions for each process:
// process 5082 // // process 5081 // // process 5080 //
printf("\nP... printf("\nP... printf("\nP...
return 0; pid_t pid3=fork(); pid_t pid2=fork();
printf("\nP... printf("\nP...
return 0; pid_t pid3=fork();
printf("\nP...
return 0;
Why is this the remaining code? any process created by a fork in some other process will start executing after that fork statement like we have seen earlier. so process 5082 printed line 5 then terminated (its value of pid3 = 0 because it is a child of 5079). After terminating of 5082, 5081 took the CPU, and it printed line 6, then created process 5085 as we can see in line 6 (why not 5083 in order? maybe the OS created some process during the execution of your code).
after printing line 6 process 5081 has terminated. Now we have 5080, and 5085 in the memory. you should now be able to follow the pattern, 5080 was selected to run, creating 5086 and 5087 then terminating. then 5085 ran, followed by 5087, which only had the print statement in the end, both then terminated, and we were left with 5086 which printed, made the last fork creating 5088, then terminated as did 5088 after its print.
Operating systems is a fascinating field, its fun to go beyond the system calls, if you are interested in this I would recommend this book, it is what I studied in college:
https://www.amazon.com/Operating-System-Concepts-Abraham-Silberschatz/dp/0470128720
Related
Right now it seems that on every click tick, the running process is preempted and forced to yield the processor, I have thoroughly investigated the code-base and the only relevant part of the code to process preemption is below (in trap.c):
// Force process to give up CPU on clock tick.
// If interrupts were on while locks held, would need to check nlock.
if(myproc() && myproc() -> state == RUNNING && tf -> trapno == T_IRQ0 + IRQ_TIMER)
yield();
I guess that timing is specified in T_IRQ0 + IRQ_TIMER, but I can't figure out how these two can be modified, these two are specified in trap.h:
#define T_IRQ0 32 // IRQ 0 corresponds to int T_IRQ
#define IRQ_TIMER 0
I wonder how I can change the default RR scheduling time-slice (which is right now 1 clock tick, fir example make it 10 clock-tick)?
If you want a process to be executed more time than the others, you can allow it more timeslices, *without` changing the timeslice duration.
To do so, you can add some extra_slice and current_slice in struct proc and modify the TIMER trap handler this way:
if(myproc() && myproc()->state == RUNNING &&
tf->trapno == T_IRQ0+IRQ_TIMER)
{
int current = myproc()->current_slice;
if ( current )
myproc()->current_slice = current - 1;
else
yield();
}
Then you just have to create a syscall to set extra_slice and modify the scheduler function to reset current_slice to extra_slice at process wakeup:
// Switch to chosen process. It is the process's job
// to release ptable.lock and then reacquire it
// before jumping back to us.
c->proc = p;
switchuvm(p);
p->state = RUNNING;
p->current_slice = p->extra_slice
You can read lapic.c file:
lapicinit(void)
{
....
// The timer repeatedly counts down at bus frequency
// from lapic[TICR] and then issues an interrupt.
// If xv6 cared more about precise timekeeping,
// TICR would be calibrated using an external time source.
lapicw(TDCR, X1);
lapicw(TIMER, PERIODIC | (T_IRQ0 + IRQ_TIMER));
lapicw(TICR, 10000000);
So, if you want the timer interrupt to be more spaced, change the TICR value:
lapicw(TICR, 10000000); //10 000 000
can become
lapicw(TICR, 100000000); //100 000 000
Warning, TICR references a 32bits unsigned counter, do not go over 4 294 967 295 (0xFFFFFFFF)
Right now it seems that on every click tick, the running process is preempted and forced to yield the processor, I have thoroughly investigated the code-base and the only relevant part of the code to process preemption is below (in trap.c):
// Force process to give up CPU on clock tick.
// If interrupts were on while locks held, would need to check nlock.
if(myproc() && myproc() -> state == RUNNING && tf -> trapno == T_IRQ0 + IRQ_TIMER)
yield();
I guess that timing is specified in T_IRQ0 + IRQ_TIMER, but I can't figure out how these two can be modified, these two are specified in trap.h:
#define T_IRQ0 32 // IRQ 0 corresponds to int T_IRQ
#define IRQ_TIMER 0
I wonder how I can change the default RR scheduling time-slice (which is right now 1 clock tick, fir example make it 10 clock-tick)?
If you want a process to be executed more time than the others, you can allow it more timeslices, *without` changing the timeslice duration.
To do so, you can add some extra_slice and current_slice in struct proc and modify the TIMER trap handler this way:
if(myproc() && myproc()->state == RUNNING &&
tf->trapno == T_IRQ0+IRQ_TIMER)
{
int current = myproc()->current_slice;
if ( current )
myproc()->current_slice = current - 1;
else
yield();
}
Then you just have to create a syscall to set extra_slice and modify the scheduler function to reset current_slice to extra_slice at process wakeup:
// Switch to chosen process. It is the process's job
// to release ptable.lock and then reacquire it
// before jumping back to us.
c->proc = p;
switchuvm(p);
p->state = RUNNING;
p->current_slice = p->extra_slice
You can read lapic.c file:
lapicinit(void)
{
....
// The timer repeatedly counts down at bus frequency
// from lapic[TICR] and then issues an interrupt.
// If xv6 cared more about precise timekeeping,
// TICR would be calibrated using an external time source.
lapicw(TDCR, X1);
lapicw(TIMER, PERIODIC | (T_IRQ0 + IRQ_TIMER));
lapicw(TICR, 10000000);
So, if you want the timer interrupt to be more spaced, change the TICR value:
lapicw(TICR, 10000000); //10 000 000
can become
lapicw(TICR, 100000000); //100 000 000
Warning, TICR references a 32bits unsigned counter, do not go over 4 294 967 295 (0xFFFFFFFF)
I am trying to call 4 tasks within another task as follows:
task execute();
logic [0:3] req1, port_select;
logic [0:3] req2;
logic [0:3] req3;
logic [0:3] req4;
logic [0:31] data11, data21;
logic [0:31] data12, data22;
logic [0:31] data13, data23;
logic [0:31] data14, data24;
bfm.reset_task();
//drive multiple ports
//repeat(1)
//begin: random_stimulus
port_select = generate_combination();
repeat(1)
begin: per_combination_iteration
//port1
req1 = port_select[0]? generate_command() : 0;
data11 = generate_data();
data21 = generate_data();
//bfm.drive_ip_port1(req,data1,data2);
//port2
req2 = port_select[1]? generate_command() : 0;
data12 = generate_data();
data22 = generate_data();
//bfm.drive_ip_port2(req,data1,data2);
//port3
req3 = port_select[2]? generate_command() : 0;
data13 = generate_data();
data23 = generate_data();
//bfm.drive_ip_port3(req,data1,data2);
//port4
req4 = port_select[3]? generate_command() : 0;
data14 = generate_data();
data24 = generate_data();
//bfm.drive_ip_port4(req,data1,data2);
fork
bfm.drive_ip_port1(req1,data11,data21);
bfm.drive_ip_port2(req2,data12,data22);
bfm.drive_ip_port3(req3,data13,data23);
bfm.drive_ip_port4(req4,data14,data24);
join
end: per_combination_iteration
//end: random_stimulus
$stop;
endtask: execute
And one of my drive_ip_port function is as follows:
//driving port2
task drive_ip_port2(input logic [0:3] req2, input logic [0:31] data1_port2, data2_port2);
req2_cmd_in = req2; //req2 command
req2_data_in = data1_port2; //req2 first operand
#200;
req2_cmd_in = 0;
req2_data_in = data2_port2; //req2 second operand
#1000;
endtask: drive_ip_port2
This is what I am trying to achieve:
I want the execute task to drive 4 ports randomly. On the first clock, I want them to send a command and data. And then on the next clock, the command should be 0 and only data need to be sent.
This is what I have tried:
As shown in my code, I have written the above code. The thought behind this code was that since tasks can handle time delays, I can call the task once and pass the data and the command and let task handle all the work.
The problem I have:
After the first clock period, I have a delay of #200(equal to my clock). Thereafter, the wire should become 0 and should remain 0 for #1000. However, I am never getting the value 0 on command. It looks like the command gets driven by this task again. I have tried using Local variables, using the watch feature, using breakpoint but still couldn't debug it. Can anyone suggest what's wrong?
I don't know why req2_cmd_in does not get set to zero. Maybe there is somewhere else an overriding assignment like a typo in another task. (Try call only one task and see what that does.)
I do know that if you want something to happen at or after a clock, wait for that clock, do not use a delay. Safest is also to make sure you start at a determined point from a clock edge. Therefore I prefer to use in my test-benches code like this:
task drive_ip_port2(input logic [0:3] req2,
input logic [0:31] data1_port2, data2_port2);
// Use this or make sure you call the task at a
// determined point from the clock
# (posedge clk) ;
// Signals here change as if they come from a clocked register
req2_cmd_in <= req2; //req2 command
req2_data_in <= data1_port2; //req2 first operand
# (posedge clk) ;
req2_cmd_in <= 0; // No command
req2_data_in <= data2_port2; // only req2 second operand
repeat (4) // 4 or 5 depends on if you wait for clock at top
# (posedge clk) ;
endtask: drive_ip_port2
As per the SV LRM section 9.3.2
for(int j=1; j <=3; ++j)
fork
automatic int k = j;
begin
.... # use k here
end
join_none
this is how to create a fork in a loop. I have tried it and it works. But if i want to create fork with join and not join_none in loop it does not work as expected but works sequentially.
How can i modify this to work using a join, i want all my forks to fork off simultaneously and then wait for all of them to finish(join) or one of them to finish(join_any)?
Thanks
If you want to wait for all of the processes fork'ed by the fork-jone_none to complete, you put a wait fork; statement after the for loop. The wait fork statements waits for all child processes of the current thread to complete.
Id there are processes created by fork-jone_none before this for loop still active that you do not want to wait for, you need to put this piece of of code in an isolation thread.
fork
some_other_process;
join_none
fork
begin : isolation_process
for(int j=1; j <=3; ++j) begin : for_loop
fork
automatic int k = j;
begin
.... # use k here
end
join_none
end : for_loop
wait fork; // will not wait for some other process
end :isolation_thread
join
To get the behavior of the fork-join_any requires some handshaking signal or event in each process to signal that it is done.
event join_any_event;
for(int j=1; j <=3; ++j) begin : for_loop
fork
automatic int k = j;
begin
.... # use k here
->> join_any_event;
end
join_none
end : for_loop
#join_any_event;
you are looking for wait fork. See IEEE Std 1800-2012 § 9.6.1 Wait fork statement
The wait fork statement blocks process execution flow until all immediate child subprocesses (processes created by the current process, excluding their descendants) have completed their execution.
Add the wait fork after your for-loop to get the desired effect:
for(int j=1; j <=3; ++j)
fork
automatic int k = j;
begin
.... # use k here
end
join_none
wait fork; // wait for all the above fork-join_none to complete
My program writenotes keeps seg faulting when I try to write a note that is too long.
./writenotes lolololololololololololololololololololololololololololololololololololololololololololololololololololololololololololololololololo
[ * ] Writing notes
Segmentation fault
Anyways, I was trying to write a python script that calls the program and curiously enough, calling it from a python script doesn't bring a seg fault, which I thought was rather peculiar.
Heres this code:
#!/usr/bin/python
from subprocess import call
call(["./writenotes", "lolololololololololololololololololololololololololololololololololololololololololololololololololololololololololololololololololo"])
Which returns
[ * ] Writing notes
Is this because of parent processing or something like such? How would calling a program through subprocess save a program from a segfault though? Are there other ways to call programs from a script that suffer seg faults?
As a note, the writenotes program was written in C. The other script is python.
You'll almost certainly find your C program is crashing but that Python is hiding that from you. Try instead with:
print call(["./writenotes", "lolololol..."])
and see what you get as a return value.
For example, this program tries to modify a string literal and, when run normally dumps core:
int main (void) {
*"xyzzy" = 'X';
return 0;
}
However, when run from the following script:
from subprocess import call
print call(["./testprog"])
I get the output -11, indicating that signal 11 (usually SIGSEGV) was raised, as per the documentation discussing Popen.returncode which subprocess.call() uses under the covers:
A negative value -N indicates that the child was terminated by signal N (Unix only).
An alternative to checking the return code is to import check_call and CalledProcessError instead of call and then use that function. It will raise an exception if the return code is non-zero.
That's probably not so important if you're only calling one executable (just get the return value in that case) but, if you're doing a lot in sequence, catching an exception from the entire group may be more readable.
Changing the C program to only crash when the first argument is 3:
#include <stdio.h>
#include <string.h>
int main (int argc, char *argv[]) {
if (argc > 1) {
printf ("hello there %s\n", argv[1]);
if (strcmp (argv[1], "3") == 0)
*"xyzzy" = 'X';
}
return 0;
}
and the script to call it with several different arguments:
from subprocess import check_call, CalledProcessError
try:
check_call(["./testprog", "0"])
check_call(["./testprog", "1"])
check_call(["./testprog", "2"])
check_call(["./testprog", "3"])
check_call(["./testprog", "4"])
check_call(["./testprog", "5"])
check_call(["./testprog", "6"])
check_call(["./testprog", "7"])
check_call(["./testprog", "8"])
check_call(["./testprog", "9"])
except CalledProcessError as e:
print e.cmd, "failed with", e.returncode
else:
print "Everything went well"
shows that in action:
hello there 0
hello there 1
hello there 2
hello there 3
['./testprog', '3'] failed with -11