-
Notifications
You must be signed in to change notification settings - Fork 1
/
Lab2_threadsc.c
96 lines (81 loc) · 2.51 KB
/
Lab2_threadsc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
/***
------------------------------------------------
threads.c R.Tervo Oct 2008
------------------------------------------------
A simple example of creating a thread.
The thread prints the uppercase alphabet.
while the main program prints a lowercase alphabet.
------------------------------------------------
compile using -lpthread
------------------------------------------------
***/
void checkresult( int result, char *text );
static void *myfunction( void *args );
static void *random_function( void *args );
//==============================================
main()
{
int result;
char *p;
pthread_t tid;
pthread_t tid2;
result = pthread_create( &tid, NULL, myfunction, NULL );
checkresult( result, "thread create failed" );
result = pthread_create(&tid2, NULL, random_function, NULL);
checkresult(result, "thread create failed");
pthread_setschedprio(tid2,9);
printf("The main's PID is: %d\n",getpid());
printf("The main's PPID is: %d\n",getppid());
for(p="abcdefghijklmnopqrstuvwxyz\n"; *p; p++ )
{ sleep(1);
putchar(*p);
fflush(stdout);
}
result = pthread_join( tid, NULL );
checkresult( result, "thread create failed" );
exit(0);
} // end main
//==============================================
static void *myfunction( void *args )
{
printf("The myfunctions's PID is: %d\n",getpid());
printf("The myfunction's PPID is: %d\n",getppid());
struct sched_param param;
pthread_getschedparam(gettid(), NULL, ¶m);
printf("Consumer assigned priority: %d\n", param.sched_priority);
printf("Consumer current priority: %d\n", param.sched_curpriority);
char *p;
for(p="ABCDEFGHIJKLMNOPQRSTUVWXYZ\n"; *p; p++ )
{ putchar(*p);
fflush(stdout);
sleep(1);
}
}
//==============================================
static void *random_function(void *args){
printf("The random_function's PID is: %d\n",getpid());
printf("The random_function's PPID is: %d\n",getppid());
struct sched_param param;
pthread_getschedparam(gettid(),NULL, ¶m);
printf("Consumer assigned priority: %d\n", param.sched_priority);
printf("Consumer current priority: %d\n", param.sched_curpriority);
int i;
for(i=0;i<26;i++){
printf("%d",i);
//fflush(stdout);
sleep(1);
}// end of for
}
//==============================================
void checkresult( int result, char *text )
{
if( result == -1 )
{
perror(text);
exit(1);
}
} // end check result