Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
N
npvp exercises
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
erib03
npvp exercises
Commits
d6abdb71
Commit
d6abdb71
authored
7 months ago
by
erib03
Browse files
Options
Downloads
Patches
Plain Diff
Upload New File
parent
be9583da
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
uebung-_6_.c
+79
-0
79 additions, 0 deletions
uebung-_6_.c
with
79 additions
and
0 deletions
uebung-_6_.c
0 → 100644
+
79
−
0
View file @
d6abdb71
// Aufgabe 1 (erfüllt aber auch Aufgabe 2)
#include
<stdio.h>
#include
<stdlib.h>
#include
<pthread.h>
#include
<unistd.h>
#define puffer 3
#define produzenten 1
#define konsumenten 1
int
puffer_array
[
puffer
];
int
einf
ü
ge_index
=
0
,
entnahme_index
=
0
,
anzahl
=
0
;
pthread_mutex_t
sperre
;
pthread_cond_t
voll
;
pthread_cond_t
leer
;
void
*
produzent
(
void
*
arg
)
{
for
(
int
i
=
0
;
i
<
5
;
i
++
)
{
int
artikel
=
rand
()
%
100
;
pthread_mutex_lock
(
&
sperre
);
while
(
anzahl
==
puffer
)
{
pthread_cond_wait
(
&
voll
,
&
sperre
);
}
puffer_array
[
einf
ü
ge_index
]
=
artikel
;
printf
(
"produzent legt %d ab
\n
"
,
artikel
);
einf
ü
ge_index
=
(
einf
ü
ge_index
+
1
)
%
puffer
;
anzahl
++
;
pthread_cond_signal
(
&
leer
);
pthread_mutex_unlock
(
&
sperre
);
sleep
(
1
);
}
return
NULL
;
}
void
*
konsument
(
void
*
arg
)
{
for
(
int
i
=
0
;
i
<
5
;
i
++
)
{
pthread_mutex_lock
(
&
sperre
);
while
(
anzahl
==
0
)
{
pthread_cond_wait
(
&
leer
,
&
sperre
);
}
int
artikel
=
puffer_array
[
entnahme_index
];
printf
(
"konsument nimmt %d
\n
"
,
artikel
);
entnahme_index
=
(
entnahme_index
+
1
)
%
puffer
;
anzahl
--
;
pthread_cond_signal
(
&
voll
);
pthread_mutex_unlock
(
&
sperre
);
sleep
(
2
);
}
return
NULL
;
}
int
main
()
{
pthread_t
produzent_thread
,
konsument_thread
;
pthread_mutex_init
(
&
sperre
,
NULL
);
pthread_cond_init
(
&
voll
,
NULL
);
pthread_cond_init
(
&
leer
,
NULL
);
pthread_create
(
&
produzent_thread
,
NULL
,
produzent
,
NULL
);
pthread_create
(
&
konsument_thread
,
NULL
,
konsument
,
NULL
);
pthread_join
(
produzent_thread
,
NULL
);
pthread_join
(
konsument_thread
,
NULL
);
return
0
;
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment