- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我一直在弄乱这个递归函数,试图获取 fork()
创建的子进程总数。不过,我似乎做对了。当我尝试使用 WEXITSTATUS()
时,程序输出变得非常不稳定。有没有办法求和这个函数中产生的子进程总数?管道是做到这一点的唯一方法,还是有更简单的方法?
它被传递“.”,以在当前工作目录中启动函数。它遍历该目录和所有子目录,只要找到子目录就 fork 。 verifyFileType()
仅检查找到的文件是否为 CSV。
**为了更清楚地编辑函数
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <limits.h>
#include <errno.h>
int traverse(char* directory)
{
struct dirent *currentDir;
DIR *traverser = opendir(directory);
if (traverser == NULL)
{
printf("Error: Could not open directory.\n");
return 0;
}
while ((currentDir = readdir(traverser)) != NULL)
{
if (currentDir->d_type == DT_DIR && strcmp(currentDir->d_name, ".") != 0 && strcmp(currentDir->d_name, "..") != 0)
{
PID = fork();
if (PID == 0)
{
char pathBuffer[1024];
snprintf(pathBuffer, sizeof(pathBuffer), "%s/%s", directory, currentDir->d_name);
traverse(pathBuffer);
//int childProc = traverse(pathBuffer);
//return childProc + 1;
exit(0);
}
else
{
//do parent stuff?
}
}
else if (strcmp(currentDir->d_name, ".") != 0 && strcmp(currentDir->d_name, "..") != 0)
{
if (verifyFileType(currentDir->d_name) == 0)
{
//if directory = '.', only send file name as fopen() arg
printf("%s%s\n", directory, currentDir->d_name);
}
}
}
if (PID > 0)
{
int status = 0;
wait(&status);
//int returned = WEXITSTATUS(status);
//return returned + 1;
}
else if (PID == -1)
{
printf("Error waiting on children. Aborting.\n");
_exit(0);
}
closedir(traverser);
return 0;
}
int main (int argc, char **argv)
{
char* beginningDir = ".";
rootPID = getpid();
/*
int procCount = 0;
procCount = traverse(beginningDir);
printf("Proc Count: %d\n", procCount);
*/
traverse(beginningDir);
return 0;
}
最佳答案
我认为你只能使用外部实体来实现,因为 fork 进程没有任何共同点(父进程除外)。
让我们使用一个文件。我们可以使用 tmpfile()
获取文件。我们需要一些锁定,使用 flock(fileno(FILE *), ...)
。每个 child 都会将一个字节写入临时文件。在所有 child 都运行后,我可以获得文件的大小 - 因此我将获得 child 的数量:
#include <stdio.h>
#include <unistd.h>
#include <assert.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/wait.h>
#include <stdbool.h>
#include <sys/file.h>
#include <sys/types.h>
#include <unistd.h>
typedef FILE cnt_t;
/**
* Create interprocess counter.
* Should be created once (and only once) by the parent process.
*/
cnt_t *cnt_new(void)
{
return tmpfile();
}
/**
* Remove interprocess counter.
* Should be called by all childs and parent
*/
void cnt_delete(cnt_t *t)
{
fclose(t);
}
void _cnt_lock(cnt_t *t)
{
for (int ret; (ret = flock(fileno(t), LOCK_EX)) != 0;) {
assert(ret == EWOULDBLOCK);
}
}
void _cnt_unlock(cnt_t *t)
{
if (flock(fileno(t), LOCK_UN) != 0) {
assert(0);
}
}
/**
* Increments counter by 1.
*/
void cnt_inc(cnt_t *t) {
assert(t != NULL);
_cnt_lock(t);
if (fwrite((char[1]){'X'}, sizeof(char), 1, t) < 0) {
assert(0);
}
if (fflush(t) != 0) {
assert(0);
}
_cnt_unlock(t);
}
void cnt_println(cnt_t *t)
{
_cnt_lock(t);
if (fseek(t, 0L, SEEK_SET) < 0) {
assert(0);
}
char buf[124];
size_t cnt = fread(buf, sizeof(char), 124, t);
printf("cnt(%p) = %ld '%.*s'\n", cnt, (void*)t, cnt, buf);
_cnt_unlock(t);
}
/**
* Get's counter value.
*/
long cnt_getCount(cnt_t *t)
{
assert(t != NULL);
_cnt_lock(t);
if (fseek(t, 0L, SEEK_END) < 0) {
assert(0);
}
const long sz = ftell(t);
if (sz < 0) {
assert(0);
}
_cnt_unlock(t);
return sz;
}
/* ----------------------------------------------------------- */
int main()
{
srand(0);
cnt_t *cntobj = cnt_new();
bool child = false;
for (int i = 0; i < 5; ++i) {
const int ret = fork();
switch (ret) {
case 0:
cnt_inc(cntobj);
child = true;
break;
case -1:
fprintf(stderr, "fork error!\n");
exit(-1);
break;
default:
fprintf(stderr, "%d -> %d\n", getpid(), ret);
break;
}
}
while (wait(NULL) != -1) continue;
if (child) {
cnt_delete(cntobj);
exit(0);
}
const long cnt = cnt_getCount(cntobj);
cnt_delete(cntobj);
fprintf(stderr, "childs %ld\n", cnt);
return 0;
}
也许我混淆太多了(typedef FILE cnt_t
看起来很奇怪),但代码有效并返回正确的数字 31。实时版本可在 jdoodle 获得.
这里有一个使用管道的解决方案:
#include <stdio.h>
#include <unistd.h>
#include <assert.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/wait.h>
#include <stdbool.h>
#include <sys/file.h>
#include <sys/types.h>
#include <unistd.h>
typedef struct {
int p[2];
} cnt_t;
/**
* Create interprocess counter.
* Should be created once (and only once) by the parent process.
*/
cnt_t *cnt_new(void)
{
cnt_t *t = malloc(sizeof(*t));
assert(t != NULL);
if (pipe(t->p) < 0) {
assert(0);
}
if (fcntl(t->p[0], F_SETFL, O_NONBLOCK) < 0) {
assert(0);
}
return t;
}
/**
* Remove interprocess counter.
* Should be called by all childs and parent
*/
void cnt_delete(cnt_t *t)
{
close(t->p[0]);
close(t->p[1]);
t->p[0] = 0;
t->p[1] = 0;
free(t);
}
/**
* Increments counter by 1.
*/
void cnt_inc(cnt_t *t)
{
assert(t != NULL);
if (write(t->p[1], (char[1]){'X'}, 1 * sizeof(char)) < 0) {
assert(0);
}
}
/**
* Get's counter value.
*/
long cnt_getCount(cnt_t *t)
{
assert(t != NULL);
char c;
long cnt = 0;
ssize_t tmp;
errno = 0;
while ((tmp = read(t->p[0], &c, 1)) == 1) {
++cnt;
}
if (tmp < 0 && errno != EWOULDBLOCK) {
assert(0);
}
const long ret = cnt;
while (cnt--) {
if (write(t->p[1], (char[1]){'X'}, 1) < 0) {
assert(0);
}
}
return ret;
}
/* ----------------------------------------------------------- */
int main()
{
srand(0);
cnt_t *cntobj = cnt_new();
bool child = false;
for (int i = 0; i < 5; ++i) {
const int ret = fork();
switch (ret) {
case 0:
cnt_inc(cntobj);
child = true;
break;
case -1:
fprintf(stderr, "fork error!\n");
exit(-1);
break;
default:
fprintf(stderr, "%d -> %d\n", getpid(), ret);
break;
}
}
while (wait(NULL) != -1) continue;
if (child) {
cnt_delete(cntobj);
exit(0);
}
const long cnt = cnt_getCount(cntobj);
cnt_delete(cntobj);
fprintf(stderr, "childs %ld\n", cnt);
return 0;
}
效果一样好,可能速度更快。现场版仍在 jdoodle .
这些示例使用 assert
进行愚蠢的错误处理,它们仅用于展示该方法以及该方法的工作原理。
也许我们还可以使用 posix 信号量和一些进程间通信、一些 shmget
和 semop
以及例如排队信号量计数来创建解决方案。
关于c - 如何从此递归函数中获取 fork() 调用的子进程总数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52955586/
+--------+-------+----------+-----------+ | Maker | Model | SeatType | NoOfSeats | +--------+------
如何使用 jQuery 计算 p 标签之间的字符数? 我尝试: DEMO html: 1 1 1 js: var tBytes = 0, tFiles = $('b').length; fo
在 MongoDB 上运行正常的“查找”查询时,我可以通过在返回的游标上运行“计数”来获得总结果计数(不考虑限制)。因此,即使我将结果集限制为 10(例如),我仍然可以知道结果总数为 53(再次,例如
在 100% 堆叠条形图中,如何让数据标签同时显示值和总百分比?示例:129 (60.3%) 当您将鼠标悬停在栏上时,它会显示在工具提示中,但在栏本身上不可见。 此处示例:https://docs.g
我在Kibana中的总和有问题。 我的用例是,我的每个服务器都会定期报告打开的 session 数。在Kibana中,我想可视化所有服务器上所有 session 的总数。但是,即使只有一台服务器联机且
我正在使用 jQuery 和 ASP.NET MVC 3 以及 razor View 引擎。 我有几个可以在其中输入数值的文本框。我有一个标签控件,其中包含由 jQuery 计算的文本框总数。 我有以
像这样的结果: 75 Ansari 5 10 88 Koodoo 4 0 90 Koodoo 14 0 83 Koodoo 5 0
关闭。这个问题需要details or clarity .它目前不接受答案。 想改进这个问题吗? 通过 editing this post 添加细节并澄清问题. 关闭 9 年前。 Improve t
我是 PHP 的初学者,我正在为我的网站编写一些代码。我想获得当时处于事件状态的 session 总数。我知道这是一项艰巨的任务,但有可能。我该怎么做? 我google了一下,有人说可以通过统计tem
1。问题陈述 我很难在正确的记录行中显示 COUNT() 的总数。 如何将 COUNT() 显示到正确的相应服务 2。背景 我想根据stage_id 和分解到project_name 显示员工负责的项
我整个下午都在尝试处理一个(或两个或三个)查询,以便获得三个表的所有子表的计数。看看我的设计: 用户表 id_user | name 1 | foo 2 | bar 获奖表 id_won | user
我有以下脚本。想要文件夹、子文件夹和文件的数量: Sub CountFiles(ByVal path1 As String) Dim fso As Object Dim subfolder As Ob
我对 c3.js 中的饼图有疑问。 如何在标题中添加饼图的总数? var title = new Array('data1.sql','data2.sql') var dtitle = new Arr
我在这方面玩得很开心。我正在尝试针对具有递归关系(分层)的表编写查询(使用 Oracle),并获取存储在树中每个节点及其下方的另一个表中的记录总数。另一个表只有与叶节点相关的记录。但是,我想获得树中每
有没有办法获取模块在任何时间点使用的绑定(bind)总数(通过模板的 {{ .. }}/ng-xxx="..." 、 $scope.$watch(...) 等)? 最佳答案 使用 document.g
我有一个非常简单的表格,因为我现在真的只是在玩 RoR,只是收集一些数据并将其插入数据库,没有什么令人兴奋的只是基本的 CRUD。但是,我想在表格的页脚中放置一个总和字段,但我在网上找不到任何接近的东
这个 mysql 查询给出了我的产品的销售数量(total 和total_staff),按一天中的天数和小时数分组。我想要每个产品的 total 和 total_staff 的总和(不按任何内容分组,
我正在尝试计算 For 循环中每个 user_name 赢得的总金额,并将其显示在 Amount Won: 之后。但是,当我运行下面的代码时,赢得金额后没有任何显示: - 它完全是空白的。我什至尝试将
我有 3 个表。产品价格、开票产品和订购产品的表格。我正在尝试创建一个连接这些的 View 。我想输出产品价格以及开票产品总数和订购产品总数。 产品价格 id season_id product
例如,我在另一个查询的 while 循环内的查询中有一个 mysql_num_rows 结果为 4,8,15,16,23,42。我的问题是如何计算 while 循环中的所有结果? (共 133 个)谢
我是一名优秀的程序员,十分优秀!