Skip to content

Commit 79ec394

Browse files
committed
added another tasks
1 parent 45f9260 commit 79ec394

File tree

7 files changed

+521
-15
lines changed

7 files changed

+521
-15
lines changed

barber.c

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#include <pthread.h>
2+
#include <semaphore.h>
3+
#include <stdio.h>
4+
#include <stdlib.h>
5+
#include <time.h>
6+
// #include <unistd.h>
7+
// #include "queue.h"
8+
9+
#define QUEUE_SIZE 4
10+
#define CLIENTS_AMOUNT 10
11+
12+
#define MAX_RAND_INT 100
13+
#define MIN_RAND_INT 0
14+
15+
int getRandom()
16+
{
17+
return rand() % (MAX_RAND_INT - MIN_RAND_INT) + MIN_RAND_INT;
18+
}
19+
20+
// Queue* q = new Queue();
21+
22+
int queue[QUEUE_SIZE];
23+
int pointer;
24+
sem_t online_clients;
25+
int online_clients_value;
26+
pthread_mutex_t queue_locker;
27+
pthread_mutex_t sem_value_locker;
28+
pthread_t clients[CLIENTS_AMOUNT];
29+
pthread_t barber;
30+
int done_clients = 0;
31+
32+
void* barber_worker(void* args)
33+
{
34+
printf("Barber is starting work...\n");
35+
while(1)
36+
{
37+
printf("Barber is waiting for clients...\n");
38+
sem_wait(&online_clients);
39+
40+
printf("Barber got new client...\n");
41+
42+
pthread_mutex_lock(&queue_locker);
43+
pointer--;
44+
pthread_mutex_unlock(&queue_locker);
45+
46+
printf("Barber removed client, done clients: %d\n", ++done_clients);
47+
48+
sleep(getRandom() % 3);
49+
}
50+
51+
return NULL;
52+
}
53+
54+
void* client(void* args)
55+
{
56+
short num = (short)args;
57+
sleep(getRandom() % 3);
58+
int online = 0;
59+
60+
printf("Client%d created\n", num);
61+
62+
pthread_mutex_lock(&sem_value_locker);
63+
64+
printf("Client%d getting value of online clients\n", num);
65+
66+
sem_getvalue(&online_clients, &online_clients_value);
67+
online = online_clients_value;
68+
69+
if(online < QUEUE_SIZE) {
70+
printf("Client%d will be on chair now...\n", num);
71+
sem_post(&online_clients);
72+
73+
pthread_mutex_lock(&queue_locker);
74+
queue[pointer++] = num;
75+
printf("Client%d in queue...\n", num);
76+
printf("Queue size: %d\n", pointer + 1);
77+
pthread_mutex_unlock(&queue_locker);
78+
}
79+
80+
pthread_mutex_unlock(&sem_value_locker);
81+
82+
return NULL;
83+
}
84+
85+
int main() {
86+
int i;
87+
pointer = 0;
88+
89+
sem_init(&online_clients, 0, 0);
90+
pthread_mutex_init(&queue_locker, NULL);
91+
92+
pthread_create(&barber, NULL, barber_worker, NULL);
93+
94+
for (i = 0; i < 10; ++i)
95+
{
96+
pthread_create(&(clients[i]), NULL, client, (void*)i);
97+
}
98+
99+
pthread_join(barber, NULL);
100+
101+
return 0;
102+
}

lab1.c

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#define BUF_SIZE 10
2+
#define NC 8
3+
#define NP 3
4+
#define MAX_RAND_INT 100
5+
#define MIN_RAND_INT 0
6+
7+
#include <pthread.h>
8+
#include <semaphore.h>
9+
#include <stdio.h>
10+
#include <stdlib.h>
11+
12+
typedef struct
13+
{
14+
int buf[BUF_SIZE];
15+
int pointer;
16+
sem_t mutex;
17+
sem_t full;
18+
sem_t empty;
19+
} ctrl;
20+
21+
ctrl c;
22+
23+
int getRandom()
24+
{
25+
return rand() % (MAX_RAND_INT - MIN_RAND_INT) + MIN_RAND_INT;
26+
}
27+
28+
void *producer(void * arg)
29+
{
30+
int prod_id = (int)arg;
31+
int item;
32+
33+
printf("\n\nCreated producer with id %d \n\n", prod_id);
34+
35+
while(1)
36+
{
37+
item = getRandom();
38+
printf("Produced item %d by %d\n", item, prod_id);
39+
40+
sem_wait(&c.full);
41+
sem_wait(&c.mutex);
42+
c.buf[c.pointer++] = item;
43+
printf("Added produced item %d by %d\n to buf in index %d\n", item, prod_id, c.pointer);
44+
sem_post(&c.mutex);
45+
sem_post(&c.empty);
46+
47+
sleep(getRandom() % 3);
48+
}
49+
}
50+
51+
void *consumer(void *arg)
52+
{
53+
int cons_id = (int)arg;
54+
55+
printf("\n\nCreated consumer with id %d \n\n", cons_id);
56+
57+
while(1)
58+
{
59+
sem_wait(&c.empty);
60+
sem_wait(&c.mutex);
61+
printf("Took item %d by consumer%d from buf with index %d\n", c.buf[c.pointer], cons_id, c.pointer);
62+
c.pointer--;
63+
sem_post(&c.mutex);
64+
sem_post(&c.full);
65+
66+
sleep(getRandom() % 3);
67+
}
68+
69+
}
70+
71+
void main()
72+
{
73+
pthread_t ptreads[NP];
74+
pthread_t ctreads[NC];
75+
76+
sem_init(&c.mutex, 0, 1);
77+
sem_init(&c.empty, 0, 0);
78+
sem_init(&c.full, 0, BUF_SIZE);
79+
c.pointer = 0;
80+
int i;
81+
82+
for(i = 0; i < NP; ++i)
83+
{
84+
pthread_create(&(ptreads[i]), NULL, producer, (void*)i);
85+
}
86+
87+
for(i = 0; i < NC; ++i) {
88+
pthread_create(&(ctreads[i]), NULL, consumer, (void*)i);
89+
}
90+
91+
pthread_join((ptreads[0]), NULL);
92+
93+
// for(i = 0; i < NP; ++i)
94+
// {
95+
// if(pthread_join((ptreads[i]), NULL))
96+
// {
97+
// printf("\n ERROR joining thread with i = %d\n", i);
98+
// exit(1);
99+
// }
100+
// }
101+
102+
// for(i = 0; i < NC; ++i) {
103+
// if(pthread_join((ctreads[i]), NULL)) {
104+
// printf("\n ERROR joining thread");
105+
// exit(1);
106+
// }
107+
// }
108+
}

os_lab_1_14_unix_semaphore.xcodeproj/xcuserdata/ihorkroosh.xcuserdatad/xcschemes/os_lab_1_14_unix_semaphore.xcscheme

+43
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,22 @@
55
<BuildAction
66
parallelizeBuildables = "YES"
77
buildImplicitDependencies = "YES">
8+
<BuildActionEntries>
9+
<BuildActionEntry
10+
buildForTesting = "YES"
11+
buildForRunning = "YES"
12+
buildForProfiling = "YES"
13+
buildForArchiving = "YES"
14+
buildForAnalyzing = "YES">
15+
<BuildableReference
16+
BuildableIdentifier = "primary"
17+
BlueprintIdentifier = "DB2CC4AE1A93C42B009774BC"
18+
BuildableName = "os_lab_1_14_unix_semaphore"
19+
BlueprintName = "os_lab_1_14_unix_semaphore"
20+
ReferencedContainer = "container:os_lab_1_14_unix_semaphore.xcodeproj">
21+
</BuildableReference>
22+
</BuildActionEntry>
23+
</BuildActionEntries>
824
</BuildAction>
925
<TestAction
1026
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
@@ -13,6 +29,15 @@
1329
buildConfiguration = "Debug">
1430
<Testables>
1531
</Testables>
32+
<MacroExpansion>
33+
<BuildableReference
34+
BuildableIdentifier = "primary"
35+
BlueprintIdentifier = "DB2CC4AE1A93C42B009774BC"
36+
BuildableName = "os_lab_1_14_unix_semaphore"
37+
BlueprintName = "os_lab_1_14_unix_semaphore"
38+
ReferencedContainer = "container:os_lab_1_14_unix_semaphore.xcodeproj">
39+
</BuildableReference>
40+
</MacroExpansion>
1641
</TestAction>
1742
<LaunchAction
1843
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
@@ -23,6 +48,15 @@
2348
ignoresPersistentStateOnLaunch = "NO"
2449
debugDocumentVersioning = "YES"
2550
allowLocationSimulation = "YES">
51+
<BuildableProductRunnable>
52+
<BuildableReference
53+
BuildableIdentifier = "primary"
54+
BlueprintIdentifier = "DB2CC4AE1A93C42B009774BC"
55+
BuildableName = "os_lab_1_14_unix_semaphore"
56+
BlueprintName = "os_lab_1_14_unix_semaphore"
57+
ReferencedContainer = "container:os_lab_1_14_unix_semaphore.xcodeproj">
58+
</BuildableReference>
59+
</BuildableProductRunnable>
2660
<AdditionalOptions>
2761
</AdditionalOptions>
2862
</LaunchAction>
@@ -32,6 +66,15 @@
3266
useCustomWorkingDirectory = "NO"
3367
buildConfiguration = "Release"
3468
debugDocumentVersioning = "YES">
69+
<BuildableProductRunnable>
70+
<BuildableReference
71+
BuildableIdentifier = "primary"
72+
BlueprintIdentifier = "DB2CC4AE1A93C42B009774BC"
73+
BuildableName = "os_lab_1_14_unix_semaphore"
74+
BlueprintName = "os_lab_1_14_unix_semaphore"
75+
ReferencedContainer = "container:os_lab_1_14_unix_semaphore.xcodeproj">
76+
</BuildableReference>
77+
</BuildableProductRunnable>
3578
</ProfileAction>
3679
<AnalyzeAction
3780
buildConfiguration = "Debug">

os_lab_1_14_unix_semaphore/main.cpp

-15
This file was deleted.

philosophs.c

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
//#include <stdafx.h>
2+
#include <windows.h>
3+
#include <iostream>
4+
#include <process.h>
5+
6+
using namespace std;
7+
8+
HANDLE event;
9+
10+
void Test1(void*);
11+
void Test2(void*);
12+
void Test3(void*);
13+
14+
int main()
15+
{
16+
event = CreateEvent(NULL, TRUE, FALSE, "FIRST STEP");
17+
18+
if (_beginthread(Test1, 1024, NULL) == -1)
19+
{
20+
cout << "Error begin thread " << endl;
21+
}
22+
if (_beginthread(Test2, 1024, NULL) == -1)
23+
{
24+
cout << "Error begin thread " << endl;
25+
}
26+
if (_beginthread(Test3, 1024, NULL) == -1)
27+
{
28+
cout << "Error begin thread " << endl;
29+
}
30+
31+
if(event != NULL)
32+
{
33+
Sleep(1000);
34+
SetEvent(event);
35+
Sleep(1000);
36+
37+
ResetEvent(event);
38+
CloseHandle(event);
39+
}
40+
else
41+
{
42+
cout << "error create event" << endl;
43+
}
44+
45+
return 0;
46+
}
47+
48+
void Test1(void* args)
49+
{
50+
DWORD dwWaitResult;
51+
while(dwWaitResult != WAIT_OBJECT_0)
52+
{
53+
dwWaitResult = WaitForSingleObject(event, 1);
54+
cout << "Test 1 timeout" << endl;
55+
}
56+
57+
cout << "Event Test 1" << endl;
58+
_endthread;
59+
}
60+
61+
void Test2(void* args)
62+
{
63+
DWORD dwWaitResult;
64+
while(dwWaitResult != WAIT_OBJECT_0)
65+
{
66+
dwWaitResult = WaitForSingleObject(event, 1);
67+
cout << "Test 2 timeout" << endl;
68+
}
69+
70+
cout << "Event Test 2" << endl;
71+
_endthread;
72+
}
73+
74+
void Test3(void* args)
75+
{
76+
DWORD dwWaitResult;
77+
while(dwWaitResult != WAIT_OBJECT_0)
78+
{
79+
dwWaitResult = WaitForSingleObject(event, 1);
80+
cout << "Test 3 timeout" << endl;
81+
}
82+
83+
cout << "Event Test 3" << endl;
84+
_endthread;
85+
}

0 commit comments

Comments
 (0)