简体   繁体   English

在C中动态创建sembuf结构

[英]Dynamically create sembuf structure in C

I have written a fairly simple program that finds the first N prime numbers (N provided as command line argument) by employing processes. 我编写了一个相当简单的程序,该程序通过使用进程来查找前N个素数(N作为命令行参数提供)。 Shared memory segments are used for the array of verified prime numbers, for the current candidate number being considered, and for the number of results obtained thus far (to compare against N to determine when to stop). 共享的内存段用于验证的质数数组,正在考虑的当前候选数以及到目前为止获得的结果数(与N进行比较以确定何时停止)。 Semaphores are used to avoid problems with multiple processes accessing the 3 shared memory segments. 信号量用于避免访问3个共享内存段的多个进程出现问题。

I have implemented this same program recently using threads and mutexes, and it was much less trouble. 我最近使用线程和互斥体实现了相同的程序,麻烦也少得多。 With this in mind, I know my prime number algorithm works. 考虑到这一点,我知道我的素数算法有效。 In fact, the current program using processes and semaphores works for the most part. 实际上,当前使用进程和信号量的程序大部分都是有效的。

However, an optional command line argument can be provided to specify the number of processes to use. 但是,可以提供一个可选的命令行参数来指定要使用的进程数。 This was straight forward in the threads version of the program. 在程序的线程版本中,这很简单。 This time around, however, I have a global sembuf struct with up and down arrays with elements equal to the number of processes being used. 但是,这一次,我有一个全局的sembuf结构,其中包含上下数组,其元素等于正在使用的进程数。 Right now I have them both set statically to 3 (the default) and the program works as expected; 现在,我将它们都静态设置为3(默认值),程序可以按预期工作; however, I cannot figure out how to dynamically create these sembufs with different numbers of up and down array element sizes to account for different process numbers (seeing as the struct is global). 但是,我无法弄清楚如何动态创建具有不同数量的上下数组元素大小的这些sembuf,以说明不同的进程号(请参见结构是全局的)。

I understand how to use malloc to dynamically assign array sizes (as outlined here: Set array size at runtime ), but that they are part of a struct seems to be an issue. 我了解如何使用malloc动态分配数组大小(如此处概述: 在运行时设置数组大小 ),但是它们是结构的一部分似乎是一个问题。 Here is my simple sembuf (if that is useful): 这是我的简单的sembuf(如果有用):

// Sembuf struct for semaphore information.
struct sembuf down[SLAVES], up[SLAVES];

where SLAVES is the number of spawned processes coordinating prime number investigation. 其中,SLAVES是协调素数调查的产生的进程数。 It works at the default of 3 (or any other value I manually define SLAVES to) currently, but dynamically setting these values is my goal. 目前,它的默认设置为3(或我手动将SLAVES定义为的其他任何值),但是我的目标是动态设置这些值。

Any ideas on how to do this, or a different strategy to achieve a similar outcome, would be much appreciated. 任何有关如何执行此操作的想法,或实现相似结果的不同策略,将不胜感激。

You currently have sth like this, right? 您目前有这样的事情,对不对?

struct Whatever
{
    ...
    struct sembuf down[SLAVES], up[SLAVES];
    ...
};

You should convert that to: 您应该将其转换为:

struct Whatever
{
    ...
    size_t slaves;
    struct sembuf *down, *up;
    ...
};

Then you initialize that with malloc(): 然后用malloc()初始化它:

Whatever whatever;
whatever.slaves = 100;
whatever.down = malloc(sizeof(struct sembuf) * whatever.slaves);
whatever.up = malloc(sizeof(struct sembuf) * whatever.slaves);

It can be used exactly the same way as you used it so far. 到目前为止,它的使用方式可以完全相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM