当前位置:网站首页>Assignment 1 - Hello World ! - Simple thread Creation
Assignment 1 - Hello World ! - Simple thread Creation
2022-07-27 17:33:00 【酸豆角姑娘】
Assignment 1 - Hello World ! - Simple thread Creation
Requirement
- Print the output of uname -a in the first line of the syslog file.
- Each syslog statement should have [COURSE:X][ASSIGNMENT:Y] where X corresponds to Course Number i.e. 1 in this case and Y corresponds to Assignment No.
Example - Aug 30 22:10:54 raspberrypi pthread:
[COURSE:1][ASSIGNMENT:1] YOUR DATA HERE
This is what your output should look like. Also, please note that the uname output is generated from the program and not copied from the terminal and pasted into the program as a string.
All syslog prints in this assignment and upcoming assignments shall follow this format. (The time, name of the machine, and the program name are generated by syslog, you do not need to add them in manually).
3. This assignment expects two log statements as below from the main and the thread in execution
Hello World from Main!
Hello World from Thread!
Solution
1. how to log uname -a info into syslog?
a. use system function
- using man system to get how to use this interface. please note that we need to #include <stdlib.h>
system("logger [COURSE:1][ASSIGNMENT:1]:`uname -a`");`
b. print uname struct elements (this makes things a little bit complicated)
- uname struct
struct utsname {
char sysname[]; /* Operating system name (e.g., "Linux") */
char nodename[]; /* Name within "some implementation-defined network" */
char release[]; /* Operating system release (e.g., "2.6.28") */
char version[]; /* Operating system version */
char machine[]; /* Hardware identifier */
#ifdef _GNU_SOURCE
char domainname[]; /* NIS or YP domain name */
#endif
};
reference: https://www.man7.org/linux/man-pages/man2/uname.2.html
- corresponding code
struct utsname *buf = malloc(sizeof(struct utsname));
uname(buf);
syslog(LOG_INFO, "%s %s %s %s %s GNU/Linux\n", buf->sysname, buf->nodename, buf->release, buf->version, buf->machine);
it has disadvantages since there is no operation system info in the struct.
2. how to use syslog
- openlog() and closelog()
3. the whole code
#include <pthread.h>
#include <sys/utsname.h>
#include <stdlib.h>
#include <stdio.h>
#include <sched.h>
#include <syslog.h>
#define NUM_THREADS 1
#define COURSE_INFO "[COURSE:1][ASSIGNMENT:1]"
typedef struct
{
int threadIdx;
} threadParams_t;
// POSIX thread declarations and scheduling attributes
//
pthread_t threads[NUM_THREADS];
threadParams_t threadParams[NUM_THREADS];
void *counterThread(void *threadp)
{
openlog(COURSE_INFO, LOG_CONS, LOG_USER);
syslog(LOG_INFO, "Hello World from Thread!");
closelog();
return 0;
}
int main (int argc, char *argv[])
{
int i;
struct utsname *buf = malloc(sizeof(struct utsname));
uname(buf);
openlog(COURSE_INFO, LOG_CONS, LOG_USER);
syslog(LOG_INFO, "%s %s %s %s %s GNU/Linux\n", buf->sysname, buf->nodename, buf->release, buf->version, buf->machine);
// I know we could use system call cmd to print uname, but here I keep my original code which print the uname struct elements one by one
#if (0)
system("logger [COURSE:1][ASSIGNMENT:1]: `uname -a`");
#endif
syslog(LOG_INFO, "Hello World from Main!");
for(i=0; i < NUM_THREADS; i++)
{
threadParams[i].threadIdx=i;
pthread_create(&threads[i], // pointer to thread descriptor
(void *)0, // use default attributes
counterThread, // thread function entry point
(void *)&(threadParams[i]) // parameters to pass in
);
}
for(i=0;i<NUM_THREADS;i++)
pthread_join(threads[i], NULL);
closelog();
}
边栏推荐
- Chapter 2 Introduction
- Normalization and standardization
- 函数优先顺序
- transformers-bert
- DCM11- 根据标识符写入数据服务 ($2E)的功能和配置【基于DaVinci Configurator Classic】
- [论文阅读] Rich Feature Hierarchies for Accurate Object Detection and Semantic Segmentation
- Sword finger offer 25. merge two sorted linked lists
- C#求完全数,输出水仙花以及类的使用
- 1.2、基于增量式生成遮挡与对抗抑制的行人再识别(代码理解与实验进度+报告)
- 会员卡头部组件使用文档
猜你喜欢

mysql函数汇总之系统信息函数

Understanding of basic concepts of channel capacity and channel bandwidth

Rodin installs the SMT solvers plug-in

VirtualBox: set shared folder

#yy关于鱼的英文学习

中国业务型CDP白皮书 | 爱分析报告

成年人只有一份主业是要付出代价的,被人事劝退后,我哭了一整晚

剑指 Offer 25. 合并两个排序的链表

Explore a new generation of activities to win customers, virtualization activities win a trick | manufacturer solicitation

22年PMP考试【全真敏捷试题】
随机推荐
VALN 11.9
函数优先顺序
Overview of deep active learning 2020
How powerful can top "hackers" be? Internet access without signal, expert: high-end operation!
Basic functions of pytorch tensor
Introduction to basic cesium controls
软件配置 | tigerVNC的下载、安装及配置
Built in module 10.18
An in-depth understanding of crystal oscillation circuit derived from xtalin pin and xtalout pin of single chip microcomputer
DCM11- 根据标识符写入数据服务 ($2E)的功能和配置【基于DaVinci Configurator Classic】
内置模块10.18
C243: examination ranking
Qtexttospeech class of QT realizes voice broadcast function
VALN 11.9
SQLite SQL writing method of creating table joint primary key
疫情下我离职一年,收入增长了10倍
Gestureoverlayview (gesture recognition 2)
GridView (implement table display icon)
VLAN test 2021.1.14
C# 后台GC 的前因后果