Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
cse130-section
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
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
Michael C
cse130-section
Commits
6c533cb8
Commit
6c533cb8
authored
4 years ago
by
Michael C
Browse files
Options
Downloads
Patches
Plain Diff
Add timer.c example code
parent
53f2e257
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
week-10/Makefile
+2
-2
2 additions, 2 deletions
week-10/Makefile
week-10/timer.c
+107
-0
107 additions, 0 deletions
week-10/timer.c
with
109 additions
and
2 deletions
week-10/Makefile
+
2
−
2
View file @
6c533cb8
...
...
@@ -5,9 +5,9 @@
# C compiler
CC
=
gcc
# C compiler flags
CFLAGS
=
-std
=
gnu11
-g
-Wall
-Wextra
-Wpedantic
-Wshadow
CFLAGS
=
-std
=
gnu11
-g
-Wall
-Wextra
-Wpedantic
-Wshadow
-pthread
# Target executable name
TARGET
=
start
er
TARGET
=
tim
er
all
:
$(TARGET)
...
...
This diff is collapsed.
Click to expand it.
week-10/timer.c
0 → 100644
+
107
−
0
View file @
6c533cb8
#include
<err.h>
#include
<arpa/inet.h>
#include
<netdb.h>
#include
<stdio.h>
#include
<stdlib.h>
#include
<string.h>
#include
<sys/socket.h>
#include
<sys/types.h>
#include
<unistd.h>
#include
<sys/time.h>
#include
<pthread.h>
#include
<stdbool.h>
int
ports
[
3
]
=
{
8080
,
8081
,
8082
};
struct
thread_data
{
pthread_mutex_t
*
lock
;
pthread_cond_t
*
cond_var
;
int
flag
;
};
void
*
simple
(
void
*
ptr_td
)
{
struct
thread_data
*
td
=
(
struct
thread_data
*
)
ptr_td
;
struct
timespec
ts
;
struct
timeval
now
;
while
(
true
)
{
pthread_mutex_lock
(
td
->
lock
);
memset
(
&
ts
,
0
,
sizeof
(
ts
));
gettimeofday
(
&
now
,
NULL
);
ts
.
tv_sec
=
now
.
tv_sec
+
5
;
pthread_cond_timedwait
(
td
->
cond_var
,
td
->
lock
,
&
ts
);
if
(
td
->
flag
)
{
pthread_mutex_unlock
(
td
->
lock
);
}
printf
(
"Still Waiting: %d
\n
"
,
td
->
flag
);
pthread_mutex_unlock
(
td
->
lock
);
// ----------------
struct
sockaddr_in
server_addr
;
int
connfd
=
socket
(
AF_INET
,
SOCK_STREAM
,
0
);
if
(
connfd
<
0
)
{
return
NULL
;
}
memset
(
&
server_addr
,
0
,
sizeof
(
server_addr
));
server_addr
.
sin_family
=
AF_INET
;
server_addr
.
sin_port
=
htons
(
ports
[
0
]);
inet_pton
(
AF_INET
,
"127.0.0.1"
,
&
(
server_addr
.
sin_addr
));
if
(
connect
(
connfd
,
(
struct
sockaddr
*
)
&
server_addr
,
sizeof
(
server_addr
))
<
0
)
{
return
NULL
;
}
printf
(
"Established Connection to httpserver on port: %d
\n
"
,
ports
[
0
]);
uint8_t
buffer
[
4096
];
int
ret
=
dprintf
(
connfd
,
"GET /healthcheck HTTP/1.1
\r\n\r\n
"
);
if
(
ret
<
0
)
{
return
NULL
;
}
sleep
(
1
);
int
bytes
=
read
(
connfd
,
buffer
,
sizeof
(
buffer
));
int
length
;
int
errors
;
int
entries
;
int
nscan
=
sscanf
(
buffer
,
"HTTP/1.1 200 OK
\r\n
Content-Length: %d
\r\n
%d
\n
%d"
,
&
length
,
&
errors
,
&
entries
);
printf
(
"Errors: %d
\n
Entries: %d
\n
"
,
errors
,
entries
);
}
return
NULL
;
}
int
main
()
{
pthread_mutex_t
mutex
=
PTHREAD_MUTEX_INITIALIZER
;
pthread_cond_t
cond_var
=
PTHREAD_COND_INITIALIZER
;
struct
thread_data
data
;
data
.
lock
=
&
mutex
;
data
.
cond_var
=
&
cond_var
;
pthread_t
t1
;
printf
(
"Starting Example...
\n
"
);
data
.
flag
=
0
;
pthread_create
(
&
t1
,
NULL
,
simple
,
&
data
);
printf
(
"Starting Wait
\n
"
);
getchar
();
data
.
flag
=
1
;
pthread_cond_signal
(
data
.
cond_var
);
printf
(
"Done Waiting...
\n
"
);
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