kernel get stack when signalled - linux-kernel

i write readers and writers where the kernel have to syncronize between them and block writer who already read a massage
when i am in the queue waiting I get signal so I do the fallowing
while (i_Allready_Read(myf) == ALLREADY_READ || isExistWriter == false )
//while (!(i_Allready_Read(aliveProc,current->pid)))
{
int i, is_sig = 0;
printk(KERN_INFO "\n\n*****entered set in read ******\n\n" );
if (i_Allready_Read(myf) == ALLREADY_READ )
wait_event_interruptible (readWaitQ1, !i_Allready_Read(myf));
else
wait_event_interruptible (readWaitQ1, isExistWriter);
//printk(KERN_INFO "Read Wakeup %d\n",current->pid);
for (i = 0; i < _NSIG_WORDS && !is_sig; i++)
{
is_sig = current->pending.signal.sig[i] & ~current->blocked.sig[i];
}
if (is_sig)
{
handledClose(myf);
module_put (THIS_MODULE);
return -EINTR;
}
}
return 0;//success
}
inline void handledClose(struct file *myf)//v
{
/*
*if we close the writer other writer
*would be able to enter to permissiones
*/
if (myf == writerpid )
{
isExistWriter = DOESNT_EXIST;
//printk(KERN_INFO "procfs_close : this is pid that just closed %d \n", writerpid);
}
/*
*else its a reader so our numofreaders
*need to decremented
*/
else
{
removeFromArr(myf);
numOfReaders--;
}
}
and my close does nothing ...
what did i do wrong?

Where are you waking up the wait queue?
You should be calling wake_up(readWaitQ1); somewhere. Possible after you set isExistWriter to true.

Related

Transmitting 1500KB (hex files )data over UDS using CAPL test module

I am trying to download my hex file of size 1500KB via UDS with CAPL test module,
p2 timer = 50ms
p2* timer = 5000ms
Here is snippet of my code for data transfer :
void TS_transferData()
{
byte transferData_serviceid = 0x36;
byte blockSequenceCounter = 0x1;
byte buffer[4093];
byte binarydata[4095];
long i,ret1,ret2,ret3,temp,timeout = 0,Counter = 0;
char filename[30] = "xxx.bin";
dword readaccess_handle;
diagrequest ECU_QUALIFIER.* request;
long valueleft;
readaccess_handle = OpenFileRead(filename,1);
if (readaccess_handle != 0 )
{
while( (valueleft = fileGetBinaryBlock(buffer,elcount(buffer),readaccess_handle))==4093 )
{
binarydata[0] = transferData_serviceid;
binarydata[1] = blockSequenceCounter;
for(i=0;i<elcount(buffer);i++)
{
binarydata[i+2] = buffer[i];
}
diagResize(request, elCount(binarydata));
DiagSetPrimitiveData(request,binarydata,elcount(binarydata));
DiagSendRequest(request);
write("length of binarydata %d ",elcount(binarydata));
// Wait until the request has been completely sent
ret1 = TestWaitForDiagRequestSent(request, 20000);
if(ret1 == 1) // Request sent
{
ret2=TestWaitForDiagResponse(request,50);
if(ret2==1) // Response received
{
ret3=DiagGetLastResponseCode(request); // Get the code of the response
if(ret3==-1) // Is it a positive response?
{
;
}
else
{
testStepFail(0, "4.0","Binary Datatransfer on server Failed");
break;
}
}
else if(ret2 == timeout)
{
testStepFail(0, "4.0","Binary Datatransfer on server Failed");
write("timeout occured while TestWaitForDiagResponse with block %d ",blockSequenceCounter);
}
}
else if(ret1 == timeout)
{
testStepFail(0, "4.0","Binary Datatransfer on server Failed");
write("timeout occured while TestWaitForDiagRequestSent %d ",blockSequenceCounter);
}
if(blockSequenceCounter == 255)
blockSequenceCounter = 0;
else
++blockSequenceCounter;
}
}
//handle the rest of the bytes to be transmitted
fileClose (readaccess_handle);
}
The software downloading is happening but it is taking a long.... time for download.
For TestWaitForDiagRequestSent() function any value for timeout less than 20000 is giving me timeout error.
Is there any other way I can reduce the software transfer time or where am I going wrong with calculation?
Is there any example I can refer to see How to transmit such a long data using CAPL ?
Sorry, I am a beginner to CAPL and UDS protocol.

Order closing and opening very quickly unintendedly in MQL4 when using multiple criteria with 'OR' operator

I am trying to close my trades based on 2 separate types of criteria,
Regardless of what the profit is, close the trade after 1020 seconds.
If my import value changes from -1 to 1 or vice versa, close it regardless of any other variables.
So far, my code works fine with 1 criteria, however when I add 2 if statements, MT4 does this thing where it opens and closes the trades very quickly, within every half a second, it doesn't look like its working properly.
If somebody could let me know what's making it do it, I would be grateful.
My code is below,
//Global Variables
//int Value = ReadCSVFile and get cell value(Either -1 or 1)
int Ticket;
int TicketBuy;
datetime SwitchBuy
datetime SwitchSell
void OnTick()
{
//Open Trades based on Value from CSV and if No other buy orders open
if(Value == 1){
if(getBuyOrderCount(Symbol(),magic) < 1){
Ticket == OrderSend(NULL,OP_BUY,0.01,Ask,2,0,0,NULL,magic,0,clrAliceBlue);
SwitchBuy = TimeCurrent();
}}
if(Value == (-1)){
if(SellOrderCount(Symbol(),magic) < 1){
TicketSell = OrderSend(NULL,OP_SELL,0.01,Bid,2,0,0,NULL,magic,0,clrAliceBlue);
SwitchSell = TimeCurrent();
}}
//Close Trades based on value in CSV file changing
if(Ticket > 0){
if(Value == (-1)||(TimeCurrent()>(SwitchBuy+60))){
CloseAllTradesBuy();
Ticket = 0;
Alert("Buy Trade Closed Because Probabilities Changed to -1!");
}
}
if(TicketSell > 0){
if(Value == 1||(TimeCurrent()>(SwitchBuy+60))){
CloseAllTradesSell();
TicketSell = 0;
Alert("Sell Trade Closed Because Probabilities Changed to 1!");
}
}
//Second Independent Criteria
if((SwitchBuy+1020) < TimeCurrent()){
if(OrderType() == OP_BUY){
CloseAllTradesBuy();
}
}
if((SwitchSell+1020) < TimeCurrent()){
if(OrderType() == OP_SELL){
CloseAllTradesSell();
}
}
}
//Functions to Close Trades
double CloseAllTradesBuy(){
for (int i =OrdersTotal(); i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS)==true)
if(OrderType()==OP_BUY)
if (OrderSymbol()==Symbol())
{
OrderClose(Ticket,OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),5,clrRed);
}
}
}
double CloseAllTradesSell(){
for (int i =OrdersTotal(); i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS)==true)
if(OrderType()==OP_SELL)
if (OrderSymbol()==Symbol())
{
OrderClose(TicketSell,OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),5,clrRed);
}
}
}
change this code of close orders :
//Functions to Close Trades
double CloseAllTradesBuy(){
for (int i =OrdersTotal()-1; i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS) && OrderType()==OP_BUY && OrderSymbol()==Symbol())
{
OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),5,clrRed);
}
}
}
double CloseAllTradesSell(){
for (int i =OrdersTotal()-1; i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS) && OrderType()==OP_SELL && OrderSymbol()==Symbol())
{
OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),5,clrRed);
}
}
}

how the function FutureTask.awaitdone() works?

recently,I read the source code about Java concurrent Jar,the code in FutureTask class is very hard to understand,the awaitDone method like this:
private int awaitDone(boolean timed, long nanos)
throws InterruptedException {
final long deadline = timed ? System.nanoTime() + nanos : 0L;
WaitNode q = null;
boolean queued = false;
for (;;) {
if (Thread.interrupted()) {
removeWaiter(q);
throw new InterruptedException();
}
int s = state;
if (s > COMPLETING) {
if (q != null)
q.thread = null;
return s;
}
else if (s == COMPLETING) // cannot time out yet
Thread.yield();
else if (q == null)
q = new WaitNode();
else if (!queued)
queued = UNSAFE.compareAndSwapObject(this, waitersOffset,
q.next = waiters, q);
else if (timed) {
nanos = deadline - System.nanoTime();
if (nanos <= 0L) {
removeWaiter(q);
return state;
}
LockSupport.parkNanos(this, nanos);
}
else
LockSupport.park(this);
}
}
so,the question is ,I don't understand the usage of waiters, and how is the removeWaiter method worked.
any help would be appreciated.
According to the FutureTask javadocs, the waiters datastructure is a Treiber Stack, a lock-free data structure.
When one thread notices that the future is set or cancelled, it notifies any other waiting threads they can continue (un-parks them).

IInternetProtocolRoot::Start is never called

I am working on Asynchronous Pluggable Protocol Handler according to
https://msdn.microsoft.com/en-us/library/aa767916(v=vs.85).aspx#Creating_an_Asynchro
but step 3 is not happens. Urlmon.dll is supposed to call QueryInterface on my pluggable protocol handler for its IInternetProtocol interface and it does not. I am missing something and I can not figure it out. Does anybody know what the problem?
On step 2, ParseUrl is called twice:
if( ParseAction == PARSEACTION::PARSE_SECURITY_URL ) {
if( cchResult < _countof(L"http://localhost/") ) {
return S_FALSE;
}
memcpy(pwzResult, L"http://localhost/", sizeof(L"http://localhost/"));
*pcchResult = _countof(L"http://localhost/") - 1;
return S_OK;
}
if( ParseAction == PARSEACTION::PARSE_DOMAIN ) {
if( cchResult < _countof(L"localhost") ) {
return S_FALSE;
}
memcpy(pwzResult, L"localhost", sizeof(L"localhost"));
*pcchResult = _countof(L"localhost") - 1;
return S_OK;
}
return INET_E_DEFAULT_ACTION;

C++ Visual Studio 2010 - Using Threads, Deque And Handlers

I'm trying to use multiple threads in an application that reads out of a large file.
I want to read from the file in the main thread and put each line into a deque called countedAddresses.
The I want multiple threads to pick lines out of countedAddresses and pull all of the email addresses out of them and place them in a deque called resolvedAddresses.
At the moment this all works if there is only one resolveAddressThread running, but if I run two I get heap corruption errors.
I launch the threads with:
_beginthread( resolveAddressThread, 0, NULL );
_beginthread( resolveAddressThread, 0, NULL );
Here is the thread code:
unsigned int linesCounted, linesResolved, linesProcessed;
std::deque<std::string> countedAddresses;std::deque<std::string> resolvedAddresses;
HANDLE linesCMutex, linesRMutex, linesRCMutex;
void resolveAddressThread(void* pParams)
{
string fileLine= "";
while(1)
{
WaitForSingleObject(linesCMutex, INFINITE);
WaitForSingleObject(linesRCMutex, INFINITE);
if (linesCounted>linesResolved)
{
if (countedAddresses.size()!=0)
{
fileLine = countedAddresses.front();
countedAddresses.pop_front();
ReleaseMutex(linesRCMutex);
ReleaseMutex(linesCMutex);
}
else
{
ReleaseMutex(linesRCMutex);
ReleaseMutex(linesCMutex);
Sleep(1000);
}
}
else
{ cout<<"All lines in the file have been processed."<<endl;
ReleaseMutex(linesRCMutex);
ReleaseMutex(linesCMutex);
break;}
if (fileLine.length()>1)
{
string::iterator strIter;
string buffer;
int tabCount=0;
bool useFlag =false, keepFlag = false, stopFlag = false;
buffer.clear();
for(strIter=fileLine.begin(); strIter!=fileLine.end(); ++strIter)
{
if (*strIter == '\t')
{
++tabCount;
}
if (tabCount == 10)
{
if ('\"'== *strIter)
{
useFlag=!useFlag;
}
else if (useFlag && *strIter > 0)
{
if (*strIter == '#')
{
buffer+=*strIter;
keepFlag = true;
}
else if ((*strIter ==' '||*strIter ==';')&& keepFlag)
{
if (buffer.at(buffer.length()-1)=='.')
{buffer = buffer.substr(0, buffer.length() - 1);}
WaitForSingleObject(linesRMutex, INFINITE);
resolvedAddresses.push_back(buffer);
ReleaseMutex(linesRMutex);
buffer.clear();
keepFlag = false;
}
else if (*strIter ==' '||*strIter ==';')
{
buffer.clear();
keepFlag = false;
}
else
{
buffer+=*strIter;
}
}
}
if (tabCount ==11 && keepFlag==true)
{
if (buffer.at(buffer.length()-1)=='.')
{buffer = buffer.substr(0, buffer.length() - 1);}
WaitForSingleObject(linesRMutex, INFINITE);
resolvedAddresses.push_back(buffer);
ReleaseMutex(linesRMutex);
buffer.clear();
keepFlag = false;
}
}
WaitForSingleObject(linesRCMutex, INFINITE);
++linesResolved;
ReleaseMutex(linesRCMutex);
fileLine.clear();
}
}
}
Please let me know where I am going wrong.
Thanks,
ANkh

Resources