Docsity

Sample Threads Code in Operating Systems - Lecture Slides | CS 4414, Study notes of Operating Systems

Toggle sidebar
bg1
1
CS414: Operating Systems
Outline
•Last time
Overview of Windows NT
Processes / Threads (chapt 4/5
•This time
Demos (Processes/Threads)
•Next time
More Processes/threads
Assignment #2 due
CS414: Operating Systems
Before We Start
•Assignment #2 – second part of the programming
assignment
Where we left off: we created the user-level routines, and were able
to invoke them via a user-level process, and see the debugging
statements appear on the debug window…
Now to implement the functionality…
We need to put a new field into the PCB, “IsHidden”
We need to initialize this field
We need to set/clear this field (in the body of the system call routines
inside the kernel)
We need to get the routines (used by the task manager and others)… in
psenum.c
CS414: Operating Systems
Simple UNIX fork
#include <unistd.h>
#include <stdio.h>
int
main ()
{pid_t child_pid;
printf ("Hello\n");
child_pid = fork ();
if (child_pid == -1) {
perror("fork");
exit(-1);
}
printf ("bye\n");
exit(0);
}
CS414: Operating Systems
DEMO
•Let’s see how this works on a Linux box…
Ps, top, /proc/cpuinfo
Cd (which cd) – cd is something that the shell (Bash) understands
Pipe, redirect, background, semi-colon
Env, echo $HOME, export
for x in one two three
do
Echo $x
done
CS414: Operating Systems
More Complicated
int main() {
char prgname[1024];
pid_t pid;
int status;
if (fgets(prgname, 1024, stdin) == NULL) {
printf ("Did not read program name. Aborting!\n");
exit(1);
}
prgname[strlen(prgname)-1]=0;
if ((pid = fork ()) == -1) {
perror("fork");
exit(1);
}
if (pid == 0) {
execlp(prgname,prgname,0);
printf("%d I did not find program %s\n", getpid(), prgname);
exit (1);
} else {
waitpid(pid, &status, 0);
if (status == 0) printf ("Program %s finished!\n", prgname);
}
return 0;
}
CS414: Operating Systems
DEMO
•Run the previous program on Linux
pf2

Partial preview of the text

Download Sample Threads Code in Operating Systems - Lecture Slides | CS 4414 and more Study notes Operating Systems in PDF only on Docsity!

CS414: Operating Systems

Outline

• Last time

 Overview of Windows NT

 Processes / Threads (chapt 4/

• This time

 Demos (Processes/Threads)

• Next time

More Processes/threads

Assignment #2 due

CS414: Operating Systems

Before We Start

• Assignment #2 – second part of the programming

assignment

Where we left off: we created the user-level routines, and were able

to invoke them via a user-level process, and see the debugging

statements appear on the debug window…

Now to implement the functionality…

 We need to put a new field into the PCB, “IsHidden”

 We need to initialize this field

 We need to set/clear this field (in the body of the system call routines

inside the kernel)

 We need to get the routines (used by the task manager and others)… in

psenum.c

CS414: Operating Systems

Simple UNIX fork

#include <unistd.h> #include <stdio.h> int main () { pid_t child_pid;

printf ("Hello\n"); child_pid = fork ();

if (child_pid == -1) { perror("fork"); exit(-1); } printf ("bye\n"); exit(0);

}

CS414: Operating Systems

DEMO

• Let’s see how this works on a Linux box…

Ps, top, /proc/cpuinfo

Cd (which cd) – cd is something that the shell (Bash) understands

Pipe, redirect, background, semi-colon

Env, echo $HOME, export

for x in one two three

do

Echo $x

done

CS414: Operating Systems

More Complicated

int main() { char prgname[1024]; pid_t pid; int status; if (fgets(prgname, 1024, stdin) == NULL) { printf ("Did not read program name. Aborting!\n"); exit(1); } prgname[strlen(prgname)-1]=0; if ((pid = fork ()) == -1) { perror("fork"); exit(1); } if (pid == 0) { execlp(prgname,prgname,0); printf("%d I did not find program %s\n", getpid(), prgname); exit (1); } else { waitpid(pid, &status, 0); if (status == 0) printf ("Program %s finished!\n", prgname); } return 0; } CS414: Operating Systems

DEMO

• Run the previous program on Linux

CS414: Operating Systems

Sample Pthreads Code (1/2)

// Pthreads example code // compile with: gcc -Wall pthreads_example.c -o pthreads_example -lpthread // execute with: ./pthreads_example (or "time ./pthreads_example")

#include <pthread.h> #include <stdio.h>

void thread_func (int num) { long long i,j;

printf("Thread %d executing.\n", num); j = 0; for (i=0; i < 700000000U; ++i){ ++j; } printf("Thread %d done (%lld).\n", num, j); }

CS414: Operating Systems

Sample Pthreads Code (2/2)

int main () { pthread_t thread1, thread2; pthread_t thread3, thread4;

pthread_create(&thread1, NULL, (void *) &thread_func, (void *) 1); pthread_create(&thread2, NULL, (void *) &thread_func, (void *) 2); pthread_create(&thread3, NULL, (void *) &thread_func, (void *) 3); pthread_create(&thread4, NULL, (void *) &thread_func, (void *) 4);

pthread_join(thread1, NULL); pthread_join(thread2, NULL); pthread_join(thread3, NULL); pthread_join(thread4, NULL);

return 0;

}

CS414: Operating Systems

DEMO

• Run the previous program on Linux

Hmmm.. Why is “top” saying that only one of the CPUs is pegged at

100%? Why aren’t both pegged?