C/C++ :: Using Semaphores On Critical Region - Processes Not In Correct Order
Mar 2, 2014
I'm trying to use semaphores on three different processes so that each process won't enter the critical region at the same time and also go in the order: process 1 -> process 2 -> process 3. However, everytime i run the code the process 3 keeps going before process 2.
Here's my code:
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
[Code].....
the processes should print out like this:
process 1: 100000
process 2: 300000
process 3: 600000
View 1 Replies
ADVERTISEMENT
Mar 29, 2013
I need my code to display Month but it seems my code is skipping month's and not displaying 1, 2 3. It's going 1 , 3 ,5 and so on. This is the code i have for it
Code:
int month = 1;
And later on the code i have month++ That's my first mistake and i'm not sure how to possibly fix it. My second mistake is my Interest Paid output. This is the code i have for it
Code:
interestPaid = loanBalance*i;
This is on the while loop, but i'm not sure why it's not displaying the correct amount. It should display for month 1 = 500.00 , Month 2 = 498.28 and Month 3 = 496.55 and so on... However my code display's something close to it, but not that. i is my monthly interest rate. My code for it seems to be fine. If i can fix this two things i'll be able to finish the project. This is my entire code, how i can fix it so that my month is displayed in 1, 2, 3, etc.... and not 1, 3, 5, etc... And why my interest is not displaying what my professor has on his output screen (i provided the first three months on top in bold) If i can fix those things i'll have the rest. Here's my entire code
#include <iostream>
#include <iomanip>
using namespace std;
int main ( ) {
int month = 1;
// Holds the user's amount of loan.
double loanAmount;
// Holds the user's interest rate.
double i;
[Code] ....
View 3 Replies
View Related
May 6, 2013
I want to program an advanced calculator. I'd like to enter some more complex expressions like -17+3*4*(4-sqrt(4) and i want, that mathematical operations are done the correct order, so at first 4-sqrt(4) is calculated, then 3*4*2 and then -17 is subtracted.
Problem 1: Convert a string into a mathematical calculation
Problem 2: Calculate in the correct order
How would I do that (I dont expect perfecly precoded calculators from you, just the way how to do it)
Google search just delivers primitive calculations with entry methods like
Enter first number 1
Enter operator +
Enter second number 2
3
But thats not what i want
View 2 Replies
View Related
Feb 13, 2014
You are writing a program to calculate the area for 500 different measurements for a playground company in the region. The company must calculate square footage of each prospective playground, as well as the total cost to mulch each area. The cost of mulch changes frequently, so the company would like to be able to input the current cost per square foot of mulch each time they run the program. Since the quantity of data is large, the input should come from a file called “measurements.txt”. You can assume the file includes the length of each prospective playground, followed by the width. The file will be in the following format.
450300
120210
.
.
.
Ask the user the name of the file they would like to write to, as well as the current price of mulch per square foot. Output the area of each playground, along with the total price of mulch to the file that the user specifies. The playground company would like each column of output to be 20 characters in width. Each column should have an appropriate heading in the output file. You should format your output data appropriately. For example, for the input data displayed above, along with a $2.00 per square foot mulch price, the output would be:
Area Total Mulch Price
135000 $270000.00
25200 $50400.00
View 3 Replies
View Related
Feb 27, 2014
I have come across below code to demenostrate the use of semaphores
#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
#include <stdlib.h>
#define NITER 1000000
int count = 0;
void * ThreadAdd(void * a)
[Code] ....
Why the question of syncronising threads and mutexes and semaphores come into the picture when we join threads?
Like in the above example we have joined the first thread and then second thread so that main process will pause for the first thread to finish as its joined and then main process resumes and gives control to second thread and pauses till its finished as this thread is also joined.
Then where is question of simultaneous access?
First thread does its job and ends itself.
Second thread does its job and ends itself.
I wonder why there is need to use semaphores or mutexes in this example? I am very new to Multithreading and this question bothers me as to why we have to worry about simeltaneous access when we can join threads and can make control wait till that thread ends?
View 10 Replies
View Related
Apr 27, 2014
Write a program to implement the following Pseudo Code using three semaphores.
Here is the pseudo code:
int sizeofbuffer = 10
semaphore Access = 1, Item = 0, EmptySpace = sizeofbuffer;
int nNumberOfItem = 0;
void Farmer() {
while ( true ){
semWait( EmptySpace );
[Code] .....
View 3 Replies
View Related
Mar 1, 2013
If any exception occurs in critical section code then what sort of issue which we see in terms of synchronization objects?
Before and after the critical section code, Mutex is locked and unlocked. But if any exception occurs then the waiting thread will be waiting for resource to be freed as Mutex is not unlocked due to the exception.
View 5 Replies
View Related
Apr 26, 2014
I have a problem with my C#. Trying to create the airstrip with planes heading out of the hangars and on to the road to the airstrip. Each Hangar pannel is connected with closes road to them. What i am stuck with right now is connecting the roads together so each one of them will see if the next road is free and if it is it will send the plane (colorder square for now) to it and then proceed to the next one when finaly they will line up for the final panel(airstrip) and take off.So far they are just going down the each road they are assigned with semaphore and thread.
using System;
using System.Windows.Forms;
using System.Threading;
using System.ComponentModel;
using System.Collections;
using System.Data;
using System.Drawing;
[Code] ....
Connecting the "p2" with "p5" so the graphics from "p1" the square will travel through P2 and P5 this would be great and will allow me to procede with my studies.
Also i do realise that this might not be the best ways of using concurrent Csharp and not the up to date one but this is what i have been assigned with and do not have any other options unfortunately.
How i understand this is that i need to have 3 buffers and 3 semaphores for each panel. But i do not know how to add those to each one of them and then connect it with the next one as if i try and add them it either puts a cross in the design view over that panel or says that there is limited options for this statement.
This is the way that i want to do it unfortunately it must be so. The most flexible this can be is by adding an array for semaphores and buffer ( do not know how to do that either).
View 7 Replies
View Related
Dec 11, 2012
I am using a thread in my application .. A DLL is written for In and Out instructions for hardware ICS and to read FIFO.
My code is
CCriticalSection crdll , crsec ;
UINT ThreadReceiveData(LPVOID param) {
for ( ; ; ) {
if (bTerminate) break; // bTerminate = 1 in Doc template destructor
crdll.Lock();
[Code] ....
I am confused , how and when I should use Ctitical Section ? The program works fine but I am not happy as this is main routine of the program and I have not understood it properly.
View 10 Replies
View Related
Oct 25, 2013
I am trying to make a random number generator to simulate a critical hit if the number generated is from 0-critical amount. However, every time i run the code, despite being within the parameters set by the if statements, it always runs through as a critical hit and never a regular hit.
#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdlib>
using namespace std;
int main(){
srand((unsigned)time(0));
[Code]...
There are four attempts to save time in getting a good result.
View 6 Replies
View Related
Jul 9, 2013
I am facing an issue with re entrance of one of my dll module. I had MLClient.dll which load in to program using LoadLibrary(MLClient.dll)
boost:: shared_ptr will add a lock() on this Load & initialization of library.
Due some of my requirements I want to un initialize this library and unload.?
How can i do the same with windows functions.?
How can i leave the lock on library using boost::shared_ptr.?
un load if there is a lock() on it ?
View 1 Replies
View Related
May 5, 2014
I wrote a C program that is supposed to create a certain number of child processes, each child process having to change 1 letter from a string. The string and the number of child processes are read from the keyboard. I want to do it using pipes.
It should work like this: The parent changes one letter, then the first child takes the string modified by the parent and changes one more letter. The second child takes the string modified by the first one (2 letters are already changed) and changes one more and so on. I am new to C and am not quite sure how it all works, especially pipes. Also can the children be linked between them through the pipe, or can they only be linked to the parent and it has to be something like: first child changes a letter, gives the string back to the parent and then the second child reads from there, modifies letter and gives back.
If it's like that, is there any way to make sure that this doesn't happen: Apples becomes AppleD and then AppleX and then AppleQ? For example:
Code:
Input: Apples
Output: Applex Appldx Aqpldx My problem is: I don't get any output from the children.
Here's my code:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/types.h>
#include<unistd.h>
#include <sys/wait.h>
[Code] .....
View 2 Replies
View Related
Apr 13, 2014
Say now I have a dll, loaded and run by a 32bit program. One of the things that I access from the program in the dll are several 1024x1024 int buffers. However I would like to put some data into those buffers from a external process. And I would like a separate thread in that external process for each buffer. Is there any way I can make that memory space accessible to the external process so I can use my own multi threaded memory transfers to pass that data over provided I ensure that the original process doesn't try to do anything with that data? And I would like to do this without resorting to the Read/WriteProcessMemory functions which are not threadsafe. In short, I want to set up direct memory access between the 2 programs without creating any intermediate shared memory buffers ie I want to set permissions for an existing memory space. Is this possible?
View 1 Replies
View Related
Nov 30, 2014
I don't think my processes are being created the way I want. I'm trying to create a fan of process (i.e. one parent with multiple children).
I want the parent to put together the final output image, and the children are supposed to do the edge detection on various regions of the image. Here is what I have:
Code:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include "iplib2New.c"
#define FILTER_SIZE 3
//Function prototypes
[Code] ......
View 10 Replies
View Related
Mar 29, 2012
I have two command line apps, one of them is multithreaded. I need to implement a bidirectional communication between them. Currently I can do
Code:
ProgramA 1> ProgramB
for the first programA to direct output into ProgramB, however I also need ProgramB to send messages to ProgramA.
how to implement it in a easiest way.
btw, I am on Ubuntu. I'd rather not to use third party libraries, Boost, etc.
View 7 Replies
View Related
Apr 14, 2015
how to get a list of the active processes on a computer?
What I need to do is check for a specific process to see if it's running on the system.
I just need to know if a certain process is present.
View 1 Replies
View Related
Oct 7, 2014
I want to show all running processes of windows.
my motive is whenever any running process closes/quits whether it be console or window based on windows it notify(s) me or user that some .exe has been closed.
View 1 Replies
View Related
Mar 19, 2014
I want parent and child processes to communicate in C linux using pipes. I have created two file descriptors. one for parent to child i.e. readpipe and other writepipe for viceversa. But I am getting null as output for ch and ch1 strings in my code below.
#include <stdio.h>
#include<stdlib.h>
#include
#include<unistd.h>
int main(){
pid_t pid;
[Code] .....
View 1 Replies
View Related
Nov 18, 2013
I want to create 9 child processes and eventually write from each process to another. How can I specifically create 9 child processes and leave them running? Right now, I am using a loop from i=0 to i=8 and running fork() within that loop, but each child process also runs the loop, which spawns dozens (maybe hundreds?) of processes. I specifically only want to create nine of them. How can I do this?
Code:
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
[Code].....
View 7 Replies
View Related
Jan 30, 2014
I'm working on a project that is supposed to create 3 processes. Process 1 should count from 1 to 100,000. Process 2 from 1 to 200,000. Process 3 from 1 to 300,000. I've written the basic code of it but my processes aren't printing out very accurate numbers.
Here's my code:
/*ass1*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdlib.h>
#include <unistd.h>
/* change the key number */
#define SHMKEY ((key_t) 8450)
typedef struct
[code]....
View 4 Replies
View Related
Jun 10, 2014
I am expecting to get correct answer for every value of x, n and a[i] where
-10^9 <= a[i] <= 10^9
1 <= x <= 10^18
and 1 <= n <= 10000
Every value here is an integer value
#include<cstdio>
#include<iostream>
#include<cmath>
#include <float.h>
#define sBig long long int
[Code] .....
View 1 Replies
View Related
Jun 1, 2013
Code:
#include<conio.h>#include<iostream.h>
class air_line {
private:
char name[5][100],name1;
long double opt,opt1,opt2,cnic[5],ticket_number[5],ticket_number1,cnic1;
public:
void input()
[code].....
View 1 Replies
View Related
Feb 21, 2013
Okay so I thought I had this assignment completed properly last week. Last night I found a bug while playing the game.why won't the game end when the player guesses the correct number? The game allows you to finish using the max number of guesses even though you already guessed the correct number.
Code:
#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define MAXGUESSES 6
#define MAXGAMES 20
#define MAXNUMBER 100
}
[code]....
View 9 Replies
View Related
Feb 17, 2013
I have a program that printf a float that is returned by a function like this:
Code:
printf("%3.1f
", myfloat());
...
float myfloat(void) {
code....
}
In w32 my program has been running ok, but when run in a w64, it crash at the printf. If I change the definition of the function to return double it runs ok. in printf format string - Wikipedia, the free encyclopedia , in the "type" section I see there is no definition to print a float, but %f is for double in fixed point.So the question is what is the correct way to printf a float? must I cast? or is it better to return double? if so, does it have a performance penalty?
View 4 Replies
View Related
Apr 1, 2013
Well... I observed, as a non-professional programmer that "overloading operators" has some strict rules and some conventions... so any operator can differ from another. In order to have a clearest idea, I'd like to ask you to specify, for every operator, the correct (or best) way to overload it.
There are cases where you define &operator and cases where you define operator (without "&"). There are cases where operator are defined as "friend" inside class, and other cases where operator is declared externally.
example: ostream &operator<<
(why it uses & ??)
So can we have a summary for all kind of operators?
View 19 Replies
View Related
Jun 13, 2014
#include <iostream>
#include <string>
using namespace std;
void secondq();
void thirdq();
[Code]....
View 2 Replies
View Related