Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
alp4-repo-template
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
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
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Teaching Stuff
alp4
alp4-repo-template
Commits
3f9b2653
Commit
3f9b2653
authored
May 8, 2023
by
omah03
Browse files
Options
Downloads
Patches
Plain Diff
Teil 1)
parent
1ae0dc4d
No related branches found
No related tags found
1 merge request
!4
Übung2
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
cars_1.c
+49
-0
49 additions, 0 deletions
cars_1.c
with
49 additions
and
0 deletions
cars_1.c
0 → 100644
+
49
−
0
View file @
3f9b2653
//Code compiles but without protection
#include
<stdio.h>
#include
<stdlib.h>
#include
<pthread.h>
#include
<unistd.h>
#include
<sys/wait.h>
#define crosses 100
int
car_on_bridge
=
0
;
void
*
func
(
void
*
arg
){
int
car_id
=
*
(
int
*
)
arg
;
int
crosses_counter
=
0
;
while
(
crosses_counter
<
crosses
){
while
(
car_on_bridge
==
1
){
}
printf
(
"Car %d is crossing the bridge
\n
"
,
car_id
);
car_on_bridge
++
;
usleep
(
10000
);
car_on_bridge
--
;
printf
(
"Car %d is crossing the bridge
\n
"
,
car_id
);
crosses_counter
++
;
}
return
NULL
;
}
int
main
(){
pthread_t
threads
[
2
];
int
thread_id
[
2
]
=
{
0
,
1
};
for
(
int
i
=
0
;
i
<
2
;
i
++
){
if
(
pthread_create
(
&
threads
[
i
],
NULL
,
func
,
&
thread_id
[
i
])
!=
0
){
printf
(
"Error creating thread"
);
exit
(
1
);
}
}
for
(
int
i
=
0
;
i
<
2
;
i
++
){
if
(
pthread_join
(
threads
[
i
],
NULL
)
!=
0
){
printf
(
"Error joining threads"
);
exit
(
1
);
}
}
printf
(
"All cars have crossed %d times!"
,
crosses
);
return
0
;
}
\ No newline at end of file
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