- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在进程和线程之间的同步练习中遇到了麻烦。主进程应该创建N个子进程(使它们大致在同一时间启动),而子进程又应该创建M个线程。现在这些线程必须写入文件。一段时间后T主线程应该停止子进程并调用一个函数来查找哪个进程进行了更多的访问。文件中写入的内容或主进程如何决定哪一个写入更多并不重要。问题是:什么也没写。文件已创建,但没有任何反应,我不明白为什么。我在这上面浪费了一个下午,但我仍然不明白出了什么问题(尽管我怀疑 sigaction,因为我不希望使用它来使子进程停止)。请帮忙。这是代码
编辑:问题不应该出现在 init_file() 或 parseOutput() 中,因为它们是教授给我的,经过测试后我就离开了它们没有变化
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <semaphore.h>
#include <pthread.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
// macros for error handling
#include "common.h"
#define N 10 // child process count
#define M 10 // thread per child process count
#define T 3 // time to sleep for main process
#define FILENAME "accesses.log"
#define SEMFILENAME "/sem_file"
#define SEMPROCESSESNAME "/sem_proc"
/*
* data structure required by threads
*/
typedef struct thread_args_s {
unsigned int child_id;
unsigned int thread_id;
} thread_args_t;
/*
* parameters can be set also via command-line arguments
*/
int n = N, m = M, t = T;
/* TODO: declare as many semaphores as needed to implement
* the intended semantics, and choose unique identifiers for
* them (e.g., "/mysem_critical_section") */
sem_t * sem_file;
sem_t * sem_processes;
int running = 1;
/*
* Create a named semaphore with a given name, mode and initial value.
* Also, tries to remove any pre-existing semaphore with the same name.
*/
sem_t *create_named_semaphore(const char *name, mode_t mode, unsigned int value) {
printf("[Main] Creating named semaphore %s...", name);
fflush(stdout);
// TODO
sem_unlink(name);
sem_t *ret;
ret = sem_open(name, O_CREAT, mode, value);
if(ret == SEM_FAILED)
{
fprintf(stderr, "failed to open %s", name);
exit(EXIT_FAILURE);
}
printf("done!!!\n");
return ret;
}
/*
* Ensures that an empty file with given name exists.
*/
void init_file(const char *filename) {
printf("[Main] Initializing file %s...", filename);
fflush(stdout);
int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
if (fd<0) handle_error("error while initializing file");
close(fd);
printf("closed...file correctly initialized!!!\n");
sem_file = create_named_semaphore(SEMFILENAME, 0777, 1);
}
/*
* Child's signal handler
*/
void child_sig_handler(int signum) {
running = 0;
}
void parseOutput() {
// identify the child that accessed the file most times
int* access_stats = calloc(n, sizeof(int)); // initialized with zeros
printf("[Main] Opening file %s in read-only mode...", FILENAME);
fflush(stdout);
int fd = open(FILENAME, O_RDONLY);
if (fd < 0) handle_error("error while opening output file");
printf("ok, reading it and updating access stats...");
fflush(stdout);
size_t read_bytes;
int index;
do {
read_bytes = read(fd, &index, sizeof(int));
if (read_bytes > 0)
access_stats[index]++;
} while(read_bytes > 0);
printf("ok, closing it...");
fflush(stdout);
close(fd);
printf("closed!!!\n");
int max_child_id = -1, max_accesses = -1, i;
for (i = 0; i < n; i++) {
printf("[Main] Child %d accessed file %s %d times\n", i, FILENAME, access_stats[i]);
if (access_stats[i] > max_accesses) {
max_accesses = access_stats[i];
max_child_id = i;
}
}
printf("[Main] ===> The process that accessed the file most often is %d (%d accesses)\n", max_child_id, max_accesses);
free(access_stats);
}
void* thread_function(void* x) {
thread_args_t *args = (thread_args_t*)x;
/* TODO: protect the critical section using semaphore(s) as needed */
if(sem_wait(sem_file)) handle_error("failed to wait on sem_file");
// open file, write child identity and close file
int fd = open(FILENAME, O_WRONLY | O_APPEND);
if (fd < 0) handle_error("error while opening file");
printf("[Child#%d-Thread#%d] File %s opened in append mode!!!\n", args->child_id, args->thread_id, FILENAME);
write(fd, &(args->child_id), sizeof(int));
printf("[Child#%d-Thread#%d] %d appended to file %s opened in append mode!!!\n", args->child_id, args->thread_id, args->child_id, FILENAME);
close(fd);
if(sem_post(sem_file)) handle_error("failed to release sem_file");
printf("[Child#%d-Thread#%d] File %s closed!!!\n", args->child_id, args->thread_id, FILENAME);
pthread_exit(NULL);
}
void childProcess(int child_id) {
/* TODO: each child process notifies the main process that it
* is ready, then waits to be notified from the main in order
* to start. As long as the main process does not notify a
* termination event [hint: use sem_getvalue() here], the child
* process repeatedly creates m threads that execute function
* thread_function() and waits for their completion. When a
* notification has arrived, the child process notifies the main
* process that it is about to terminate, and releases any
* shared resources before exiting. */
sem_wait(sem_processes);
int i;
pthread_t threads[M];
while(running)
{
for(i = 0; i < M; i++)
if(pthread_create(threads+i, NULL, &thread_function, NULL)) handle_error("Failed to create pthread");
for(; i > 0; i ++)
if(pthread_join(threads[i], NULL)) handle_error("Failed to join pthread");
}
_exit(EXIT_SUCCESS);
}
void mainProcess() {
/* TODO: the main process waits for all the children to start,
* it notifies them to start their activities, and sleeps
* for some time t. Once it wakes up, it notifies the children
* to end their activities, and waits for their termination.
* Finally, it calls the parseOutput() method and releases
* any shared resources. */
int i;
unsigned t_left = T;
pid_t id, *childs_ids = malloc(sizeof(pid_t)*N);
sem_processes = create_named_semaphore(SEMPROCESSESNAME, 0777, 0);
struct sigaction act;
memset(&act, 0, sizeof(act));
act.sa_handler = &child_sig_handler;
sigaction(SIGTERM, &act, NULL);
for(i = 0; i < N; i++)
{
id = fork();
if(id < 0)
handle_error("Fork failed");
if(id == 0)
childProcess(i);
childs_ids[i] = id;
}
for(; i > 0; i--)
sem_post(sem_processes);
//Sleeps for T seconds
while((t_left = sleep(t_left)));
for(; i < N; i++)
{
kill(childs_ids[i], SIGTERM);
}
int status;
for(; i>0; i--)
if(wait(&status)< 0) handle_error("wait failed");
parseOutput();
}
int main(int argc, char **argv) {
// arguments
if (argc > 1) n = atoi(argv[1]);
if (argc > 2) m = atoi(argv[2]);
if (argc > 3) t = atoi(argv[3]);
// initialize the file
init_file(FILENAME);
/* TODO: initialize any semaphore needed in the implementation, and
* create N children where the i-th child calls childProcess(i); then
* the main process executes function mainProcess() once all the
* children have been created */
mainProcess();
if(sem_close(sem_processes)) handle_error("failed to close sem_processes");
if(sem_close(sem_file)) handle_error("failed to close sem_file");
if(sem_unlink(SEMPROCESSESNAME)) handle_error("failed to unlink sem_processes");
if(sem_unlink(SEMFILENAME)) handle_error("failed to unlink sem_file");
exit(EXIT_SUCCESS);
}
这是 shell 上的输出:
[Main] Child 0 accessed file accesses.log 0 times
[Main] Child 1 accessed file accesses.log 0 times
[Main] Child 2 accessed file accesses.log 0 times
[Main] Child 3 accessed file accesses.log 0 times
[Main] Child 4 accessed file accesses.log 0 times
[Main] Child 5 accessed file accesses.log 0 times
[Main] Child 6 accessed file accesses.log 0 times
[Main] Child 7 accessed file accesses.log 0 times
[Main] Child 8 accessed file accesses.log 0 times
[Main] Child 9 accessed file accesses.log 0 times
[Main] Child 10 accessed file accesses.log 0 times
[Main] Child 11 accessed file accesses.log 0 times
[Main] Child 12 accessed file accesses.log 0 times
[Main] Child 13 accessed file accesses.log 0 times
[Main] Child 14 accessed file accesses.log 0 times
[Main] Child 15 accessed file accesses.log 0 times
[Main] Child 16 accessed file accesses.log 0 times
[Main] Child 17 accessed file accesses.log 0 times
[Main] Child 18 accessed file accesses.log 0 times
[Main] Child 19 accessed file accesses.log 0 times
[Main] Child 20 accessed file accesses.log 0 times
[Main] Child 21 accessed file accesses.log 0 times
[Main] Child 22 accessed file accesses.log 0 times
[Main] Child 23 accessed file accesses.log 0 times
[Main] Child 24 accessed file accesses.log 0 times
[Main] Child 25 accessed file accesses.log 0 times
[Main] Child 26 accessed file accesses.log 0 times
[Main] Child 27 accessed file accesses.log 0 times
[Main] Child 28 accessed file accesses.log 0 times
[Main] Child 29 accessed file accesses.log 0 times
[Main] Child 30 accessed file accesses.log 0 times
[Main] Child 31 accessed file accesses.log 0 times
[Main] Child 32 accessed file accesses.log 0 times
[Main] Child 33 accessed file accesses.log 0 times
[Main] Child 34 accessed file accesses.log 0 times
[Main] Child 35 accessed file accesses.log 0 times
[Main] Child 36 accessed file accesses.log 0 times
[Main] Child 37 accessed file accesses.log 0 times
[Main] Child 38 accessed file accesses.log 0 times
[Main] Child 39 accessed file accesses.log 0 times
[Main] Child 40 accessed file accesses.log 0 times
[Main] Child 41 accessed file accesses.log 0 times
[Main] Child 42 accessed file accesses.log 0 times
[Main] Child 43 accessed file accesses.log 0 times
[Main] Child 44 accessed file accesses.log 0 times
[Main] Child 45 accessed file accesses.log 0 times
[Main] Child 46 accessed file accesses.log 0 times
[Main] Child 47 accessed file accesses.log 0 times
[Main] Child 48 accessed file accesses.log 0 times
[Main] Child 49 accessed file accesses.log 0 times
[Main] Child 50 accessed file accesses.log 0 times
[Main] Child 51 accessed file accesses.log 0 times
[Main] Child 52 accessed file accesses.log 0 times
[Main] Child 53 accessed file accesses.log 0 times
[Main] Child 54 accessed file accesses.log 0 times
[Main] Child 55 accessed file accesses.log 0 times
[Main] Child 56 accessed file accesses.log 0 times
[Main] Child 57 accessed file accesses.log 0 times
[Main] Child 58 accessed file accesses.log 0 times
[Main] Child 59 accessed file accesses.log 0 times
[Main] Child 60 accessed file accesses.log 0 times
[Main] Child 61 accessed file accesses.log 0 times
[Main] Child 62 accessed file accesses.log 0 times
[Main] Child 63 accessed file accesses.log 0 times
[Main] Child 64 accessed file accesses.log 0 times
[Main] Child 65 accessed file accesses.log 0 times
[Main] Child 66 accessed file accesses.log 0 times
[Main] Child 67 accessed file accesses.log 0 times
[Main] Child 68 accessed file accesses.log 0 times
[Main] Child 69 accessed file accesses.log 0 times
[Main] Child 70 accessed file accesses.log 0 times
[Main] Child 71 accessed file accesses.log 0 times
[Main] Child 72 accessed file accesses.log 0 times
[Main] Child 73 accessed file accesses.log 0 times
[Main] Child 74 accessed file accesses.log 0 times
[Main] Child 75 accessed file accesses.log 0 times
[Main] Child 76 accessed file accesses.log 0 times
[Main] Child 77 accessed file accesses.log 0 times
[Main] Child 78 accessed file accesses.log 0 times
[Main] Child 79 accessed file accesses.log 0 times
[Main] Child 80 accessed file accesses.log 0 times
[Main] Child 81 accessed file accesses.log 0 times
[Main] Child 82 accessed file accesses.log 0 times
[Main] Child 83 accessed file accesses.log 0 times
[Main] Child 84 accessed file accesses.log 0 times
[Main] Child 85 accessed file accesses.log 0 times
[Main] Child 86 accessed file accesses.log 0 times
[Main] Child 87 accessed file accesses.log 0 times
[Main] Child 88 accessed file accesses.log 0 times
[Main] Child 89 accessed file accesses.log 0 times
[Main] Child 90 accessed file accesses.log 0 times
[Main] Child 91 accessed file accesses.log 0 times
[Main] Child 92 accessed file accesses.log 0 times
[Main] Child 93 accessed file accesses.log 0 times
[Main] Child 94 accessed file accesses.log 0 times
[Main] Child 95 accessed file accesses.log 0 times
[Main] Child 96 accessed file accesses.log 0 times
[Main] Child 97 accessed file accesses.log 0 times
[Main] Child 98 accessed file accesses.log 0 times
[Main] Child 99 accessed file accesses.log 0 times
[Main] ===> The process that accessed the file most often is 0 (0 accesses)
这里也是common.h
#include <errno.h>
#include <stdio.h>
#include <string.h>
// macros for handling errors
#define handle_error_en(en, msg) do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
#define handle_error(msg) do { perror(msg); exit(EXIT_FAILURE); } while (0)
最佳答案
线程函数需要一个类型为 thread_args_t 的参数,而我传递的是 NULL。我什至不知道为什么它从未引发段错误。通过对 pthread_create 的正确输入,一切正常。
关于c - 使用多个进程和 pthread 处理文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50004029/
今天我在一个 Java 应用程序中看到了几种不同的加载文件的方法。 文件:/ 文件:// 文件:/// 这三个 URL 开头有什么区别?使用它们的首选方式是什么? 非常感谢 斯特凡 最佳答案 file
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
我有一个 javascript 文件,并且在该方法中有一个“测试”方法,我喜欢调用 C# 函数。 c# 函数与 javascript 文件不在同一文件中。 它位于 .cs 文件中。那么我该如何管理 j
需要检查我使用的文件/目录的权限 //filePath = path of file/directory access denied by user ( in windows ) File fil
我在一个目录中有很多 java 文件,我想在我的 Intellij 项目中使用它。但是我不想每次开始一个新项目时都将 java 文件复制到我的项目中。 我知道我可以在 Visual Studio 和
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a software
我有 3 个组件的 Twig 文件: 文件 1: {# content-here #} 文件 2: {{ title-here }} {# content-here #}
我得到了 mod_ldap.c 和 mod_authnz_ldap.c 文件。我需要使用 Linux 命令的 mod_ldap.so 和 mod_authnz_ldap.so 文件。 最佳答案 从 c
我想使用PIE在我的项目中使用 IE7。 但是我不明白的是,我只能在网络服务器上使用 .htc 文件吗? 我可以在没有网络服务器的情况下通过浏览器加载的本地页面中使用它吗? 我在 PIE 的文档中看到
我在 CI 管道中考虑这一点,我应该首先构建和测试我的应用程序,结果应该是一个 docker 镜像。 我想知道使用构建环境在构建服务器上构建然后运行测试是否更常见。也许为此使用构建脚本。最后只需将 j
using namespace std; struct WebSites { string siteName; int rank; string getSiteName() {
我是 Linux 新手,目前正在尝试使用 ginkgo USB-CAN 接口(interface) 的 API 编程功能。为了使用 C++ 对 API 进行编程,他们提供了库文件,其中包含三个带有 .
我刚学C语言,在实现一个程序时遇到了问题将 test.txt 文件作为程序的输入。 test.txt 文件的内容是: 1 30 30 40 50 60 2 40 30 50 60 60 3 30 20
如何连接两个tcpdump文件,使一个流量在文件中出现一个接一个?具体来说,我想“乘以”一个 tcpdump 文件,这样所有的 session 将一个接一个地按顺序重复几次。 最佳答案 mergeca
我有一个名为 input.MP4 的文件,它已损坏。它来自闭路电视摄像机。我什么都试过了,ffmpeg , VLC 转换,没有运气。但是,我使用了 mediainfo和 exiftool并提取以下信息
我想做什么? 我想提取 ISO 文件并编辑其中的文件,然后将其重新打包回 ISO 文件。 (正如你已经读过的) 我为什么要这样做? 我想开始修改 PSP ISO,为此我必须使用游戏资源、 Assets
给定一个 gzip 文件 Z,如果我将其解压缩为 Z',有什么办法可以重新压缩它以恢复完全相同的 gzip 文件 Z?在粗略阅读了 DEFLATE 格式后,我猜不会,因为任何给定的文件都可能在 DEF
我必须从数据库向我的邮件 ID 发送一封带有附件的邮件。 EXEC msdb.dbo.sp_send_dbmail @profile_name = 'Adventure Works Admin
我有一个大的 M4B 文件和一个 CUE 文件。我想将其拆分为多个 M4B 文件,或将其拆分为多个 MP3 文件(以前首选)。 我想在命令行中执行此操作(OS X,但如果需要可以使用 Linux),而
快速提问。我有一个没有实现文件的类的项目。 然后在 AppDelegate 我有: #import "AppDelegate.h" #import "SomeClass.h" @interface A
我是一名优秀的程序员,十分优秀!